ios - 在 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
}
解决方案
您的问题是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()
推荐阅读
- reactjs - i18next-xhr-backend 回退以在反应应用程序中出现网络错误后加载本地资源
- android - 卸载应用程序后卡在 Flutter 中安装 build\app\outputs\apk\app.apk...
- python - AttributeError:“DataFrame”对象没有属性“write”
- reactjs - Redux 是整个应用程序的单点故障吗?
- ionic-framework - 我正在处理离子项目在将离子应用程序中的视频压缩和水印上传到服务器之前
- java - 有没有办法在appium中有两个手指触摸?
- javascript - 如何在java脚本中同步音频和文本文件?
- mysql - 连接表并在mysql中的两个diff表中搜索数据
- sql - 使用表 B 的左连接返回 1 个结果
- c# - 是否可以从 C# 中的字符串创建属性?