Ios 在UITableView中自定义每个单元格的分隔符
我在Ios 在UITableView中自定义每个单元格的分隔符,ios,swift,uitableview,Ios,Swift,Uitableview,我在UITableView中显示项目列表,其中只有第一行有分隔符,其余行没有任何分隔符。对于单元格的其余部分,我添加了: cell.separatorInset = UIEdgeInsets(top: 0, left: cell.bounds.size.width, bottom: 0, right: 0) 这将删除除第一行以外的所有行的分隔符。但当应用程序启动时,除第一个加载的单元格外,所有单元格都有如下部分分隔符: 加载底部行时滚动时,不存在此问题: 当我将所有第一次加载的行从屏幕中滚动出
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 indexath: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类中,使用prepareforuse()方法
将
separatorInset=uiedgeinset(顶部:0,左侧:cell.bounds.size.width,底部:0,右侧:0)
放在该方法内部。(您期望的默认行为)您的问题是执行此代码时,cell.bounds.size
不正确,因此您可以使用self.view.bound.size.width
使用UIViewController.view
bounds或直接.greatestfinitemagnity
使用最后一个
修改后的代码应该是这样的
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
}
extension UITableViewCell {
func hideSeparator() {
self.separatorInset = UIEdgeInsets(top: 0, left: .greatestFiniteMagnitude, bottom: 0, right: 0)
if #available(iOS 11.0, *) {
self.directionalLayoutMargins = .zero
}
}
}
作为备选方案,您可以为所有TableViewCells设置扩展名
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
}
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()
你能提供一个可执行的演示吗?或者在
func tableView(\uTableView:UITableView,cellForRowAt indexPath:indexPath)->UITableViewCell
中提供完整的代码,使用.greatestFiniteMagnitude
而不是cell.bounds.size
执行此代码时未定义您的单元格边界,这就是为什么出现此问题的原因,这解决了我的问题。这会产生与以前类似的结果。@SoumyaMahunt我个人更喜欢用作扩展;)致以最良好的祝愿,祝你幸福coding@ReinerMelian,我有一个新问题,当显示的单元格少于适合屏幕的单元格时,我可以看到其余单元格的分隔符:。任何解决方法???@SoumyaMahunt您需要在viewController viewDidLoad上设置self.tableView.tableFooterView=UIView()