首頁  >  問答  >  主體

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

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

高洛峰高洛峰2721 天前860

全部回覆(7)我來回復

  • 黄舟

    黄舟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)
    }

    回覆
    0
  • 巴扎黑

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

    每個 cell 高度高一點點(高出你想要的間距),cell 背景設為透明,用自己的 view 作為背景,不填滿 cell ,下面空出 你超出的高度

    回覆
    0
  • 黄舟

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

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

    回覆
    0
  • 天蓬老师

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

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

    回覆
    0
  • 天蓬老师

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

    可以設定多個section啊,每個section就一條cell,那麼cell之間的距離不就變成設定section之間的距離了。
    section之間的距離不好設麼

    回覆
    0
  • 黄舟

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

    1樓的方法是最可靠的,collectionView性能比較可憐,不如cell多設置一些空白比較划算。

    回覆
    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];
    }

    回覆
    0
  • 取消回覆