search

Home  >  Q&A  >  body text

uitableview - iOS 中 tableView 怎么设置Cell与Cell 之间的间距?

不是 heightForRowAtIndexPath: 这个的间距,我指的是cell与cell之间的间距,默认是贴紧的!!!!

高洛峰高洛峰2835 days ago938

reply all(7)I'll reply

  • 黄舟

    黄舟2017-04-17 17:30:45

    You mean. . Just like in Weibo. . Does it seem like there are gaps between each cell?

    Stupid method, set a little blank area in each cell X D

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 60.0f
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
        cell.contentView.backgroundColor = UIColor.clearColor()
    
        let gapView : UIView = UIView(frame: CGRectMake(0, 10, self.view.frame.size.width, 120))
    
        gapView.layer.backgroundColor = UIColor(red: 1.0f, green: 1.0f, blue: 1.0f, alpha: 1.0f)
        gapView.layer.masksToBounds = false
        gapView.layer.cornerRadius = 2.0f
        gapView.layer.shadowOffset = CGSizeMake(-1, 1)
        gapView.layer.shadowOpacity = 0.2f
    
        cell.contentView.addSubview(gapView)
        cell.contentView.sendSubviewToBack(gapView)
    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:30:45

    The height of each cell is a little higher (higher than the spacing you want), the cell background is set to transparent, use your own view as the background, do not fill the cell, and leave the height you exceed

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:30:45

    UITableView只能通过间接的方式设置出来。
    如果想更完美智能地设置cell间距,建议你使用UICollectionView

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:30:45

    It would be better if you use heightForRowAtIndexPath:to leave a little more height...

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:30:45

    You can set multiple sections. Each section has one cell, so the distance between cells becomes the distance between sections.
    Is it difficult to set the distance between sections?

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:30:45

    The method on the first floor is the most reliable. The performance of collectionView is relatively poor. It is more cost-effective to set more blanks in the cell.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:30:45

    Rewrite the setFrame method of cell

    MessageCellSectionMargin customizes a value

    - (void)setFrame:(CGRect)frame {
        
        CGRect cellFrame = frame;
        cellFrame.size.height = cellFrame.size.height - MessageCellSectionMargin;
        cellFrame.origin.y = cellFrame.origin.y + MessageCellSectionMargin;
        frame = cellFrame;
        
        [super setFrame:frame];
    }

    reply
    0
  • Cancelreply