uitableview - iOS 中 tableView 怎么设置Cell与Cell 之间的间距?
高洛峰
高洛峰 2017-04-17 17:28:45
0
7
905

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(7)
黄舟

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)
}
巴扎黑

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

黄舟

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

Peter_Zhu

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

Peter_Zhu

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?

黄舟

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.

Peter_Zhu

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];
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template