Home  >  Q&A  >  body text

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

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

高洛峰高洛峰2626 days ago778

reply all(7)I'll reply

  • 黄舟

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

    你的意思是。。就像微博里面那样。。好像每个 cell 之间有间隙那种么?

    笨方法,每个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

    每个 cell 高度高一点点(高出你想要的间距),cell 背景设为透明,用自己的 view 作为背景,不填满 cell ,下面空出 你超出的高度

    reply
    0
  • 黄舟

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

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

    reply
    0
  • 天蓬老师

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

    你用 heightForRowAtIndexPath:多留出一点高度不就好了……

    reply
    0
  • 天蓬老师

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

    可以设置多个section啊,每个section就一条cell,那么cell之间的距离不就变成设置section之间的距离了。
    section之间的距离不好设么

    reply
    0
  • 黄舟

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

    1楼的办法是最靠谱的,collectionView性能比较可怜,还不如cell多设置一些空白比较划算。

    reply
    0
  • 天蓬老师

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

    重写cell的setFrame方法

    MessageCellSectionMargin自定义一个数值

    - (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