【问题标题】:Customize separator per cell in UITableView在 UITableView 中自定义每个单元格的分隔符
【发布时间】:2020-11-01 10:17:17
【问题描述】:

我在UITableView 中显示项目列表,其中只有第一行有分隔符,其余行没有任何分隔符。对于我添加的单元格的其余部分:

cell.separatorInset = UIEdgeInsets(top: 0, left: cell.bounds.size.width, bottom: 0, right: 0)

这应该删除除第一行以外的所有行的分隔符。但是当应用程序启动时,除了第一个加载的单元格之外的所有单元格都有部分分隔符,如下所示: 在加载底部行时滚动时没有这个问题: 当我将所有第一个加载的行滚动出屏幕并滚动回它们时,所有部分分隔符都消失了: 我似乎无法找到解决此问题的方法。有没有其他方法可以通过将它们的颜色设置为与背景颜色相同来使分隔符消失?我的 xcode 版本是 11.7,swift 语言版本是 5。

更新 1

按照要求func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 看起来像:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = countryCodeView.dequeueReusableCell(withIdentifier: "CountryCell") as! CountryCell
        let country = countries[indexPath.row]

        cell.flag.text = country.flag
        cell.countryCode.text = "+\(country.countryCode)"
        cell.countryName.text = country.name
        
        if indexPath.row == 0 {
            cell.countryCode.font = UIFont(name: "ProximaNovaA-Bold", size: 16)
            cell.countryName.font = UIFont(name: "ProximaNovaA-Bold", size: 16)
            cell.separatorInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
            cell.tickIcon.isHidden = false
        } else {
            cell.countryCode.font = UIFont(name: "ProximaNovaA-Regular", size: 16)
            cell.countryName.font = UIFont(name: "ProximaNovaA-Regular", size: 16)
            cell.separatorInset = UIEdgeInsets(top: 0, left: cell.bounds.size.width, bottom: 0, right: 0)
            cell.tickIcon.isHidden = true
        }
        
        return cell
    }

【问题讨论】:

  • 你能提供一个关于它的可执行演示吗?或在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中提供完整代码
  • 使用 .greatestFiniteMagnitude 而不是 cell.bounds.size 执行此代码时未定义您的单元格边界,这就是您遇到此问题的原因
  • 感谢@ReinierMelian,解决了我的问题。

标签: ios swift uitableview


【解决方案1】:

你的问题是这段代码执行时cell.bounds.size不正确,所以你可以使用self.view.bound.size.width使用UIViewController.viewbounds或者直接.greatestFiniteMagnitude我更喜欢使用最后一个

修改后的代码应该是这样的

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = countryCodeView.dequeueReusableCell(withIdentifier: "CountryCell") as! CountryCell
        let country = countries[indexPath.row]

        cell.flag.text = country.flag
        cell.countryCode.text = "+\(country.countryCode)"
        cell.countryName.text = country.name
        
        if indexPath.row == 0 {
            cell.countryCode.font = UIFont(name: "ProximaNovaA-Bold", size: 16)
            cell.countryName.font = UIFont(name: "ProximaNovaA-Bold", size: 16)
            cell.separatorInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
            cell.tickIcon.isHidden = false
        } else {
            cell.countryCode.font = UIFont(name: "ProximaNovaA-Regular", size: 16)
            cell.countryName.font = UIFont(name: "ProximaNovaA-Regular", size: 16)
            cell.separatorInset = UIEdgeInsets(top: 0, left: .greatestFiniteMagnitude, bottom: 0, right: 0)
            cell.tickIcon.isHidden = true
        }
        
        return cell
    }

作为替代方案,您可以为所有 TableViewCells 扩展

extension UITableViewCell {
    func hideSeparator() {
        self.separatorInset = UIEdgeInsets(top: 0, left: .greatestFiniteMagnitude, bottom: 0, right: 0)
        if #available(iOS 11.0, *) {
            self.directionalLayoutMargins = .zero
        }
    }
}

只需在需要隐藏分隔符的单元格上调用此方法即可使用此方法

cell.hideSeparator()

【讨论】:

  • @SoumyaMahunt 我个人更喜欢用作扩展名;)最好的问候和快乐的编码
  • @Reiner Melian,我遇到了一个新问题,当显示的单元格少于可以适应屏幕的单元格时,我可以看到其余单元格的分隔符:i.stack.imgur.com/08nlv.png。有什么办法吗??
  • @SoumyaMahunt 你需要在你的 viewController viewDidLoad 上设置self.tableView.tableFooterView = UIView()
【解决方案2】:

在单元类中使用 prepareForReuse() 方法

separatorInset = UIEdgeInsets(top: 0, left: cell.bounds.size.width, bottom: 0, right: 0) 放入该方法中。 (您期望的默认行为)

【讨论】:

  • 这会产生与以前相似的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多