简体   繁体   English

如何将UITableView上的分隔线放到特定部分?

[英]How to put separator line on UITableView to specific section?

I've a UITableView with some sections. 我有一些部分的UITableView I want that just one section had separator lines for rows. 我只希望一个部分的行有分隔线。

I tried this: 我尝试了这个:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    if indexPath.section == 1 {
        cell.separatorInset = UIEdgeInsetsMake(0, 1, 0, 0) // With other values too.
    }
    ...
    return cell
}

but nothing happens. 但什么也没发生。

You can create an extension to UITableViewCell: 您可以创建UITableViewCell的扩展:

extension UITableViewCell {

  func hideSeparator() {
    self.separatorInset = UIEdgeInsets(top: 0, left: self.bounds.size.width, bottom: 0, right: 0)
  }

  func showSeparator() {
    self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  }
}

So you can use it in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 因此,您可以在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)使用它

if indexPath.section == 1 { 
    cell.hideSeparator()
} else {
    cell.showSeparator()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM