首页 > 解决方案 > 在 UITableView 中自定义每个单元格的分隔符

问题描述

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

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
    }

标签: iosswiftuitableview

解决方案


您的问题是cell.bounds.size执行此代码时不正确,因此您可以使用self.view.bound.size.width边界UIViewController.view或直接.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()

推荐阅读