UITableView: the proper way to display a separator for the last cell

后端 未结 14 1058
礼貌的吻别
礼貌的吻别 2020-12-29 02:23

The question is what\'s the right-most way to display a separator in the last cell in a table/section.

Basically this is what I am after.

14条回答
  •  抹茶落季
    2020-12-29 03:07

    Swift 3

    I found this old issue, so I tried the same in Swift:

        tableView.tableFooterView = UIView()
        tableView.tableFooterView?.height = 0 // Maybe not necessary
    

    But it lead to another problem (in my case). So I solved it differently. I added an extra cell at the end of this section, empty and with height equal to zero:

        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return previousNumberOfRows + 1
        }
    
        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return indexPath.row == previousNumberOfRows ? 0 : previousCellHeight
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          if indexPath.row == items.count {
            return UITableViewCell()
        } else {
           previousCellInitialization()
        }
    

    This is a less concise solution, but works in more cases, if you have multiple sections, existing footer view(s)...

提交回复
热议问题