【发布时间】: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