gpt4 book ai didi

ios - 在 UITableView 中自定义每个单元格的分隔符

转载 作者:行者123 更新时间:2023-12-04 08:39:32 25 4
gpt4 key购买 nike

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

cell.separatorInset = UIEdgeInsets(top: 0, left: cell.bounds.size.width, bottom: 0, right: 0)
这应该删除除第一行以外的所有行的分隔符。但是当应用程序启动时,除了第一个加载的单元格之外的所有单元格都有这样的部分分隔符:
partial separators on first launch
在加载底部行时滚动时没有此问题:
no separators in bottom rest rows
当我将所有第一个加载的行滚动出屏幕并滚动回它们时,所有部分分隔符都消失了:
reloading first-loaded rows removes separators
我似乎无法找到解决此问题的方法。有没有其他方法可以通过将分隔符的颜色设置为与背景颜色相同来使分隔符消失?我的 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
}

最佳答案

你的问题是 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
}
作为替代方案,您可以为所有 TableViewCell 扩展
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()

关于ios - 在 UITableView 中自定义每个单元格的分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64630752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com