下面的代码是工作的,但当我多次滚动tableview时,其他单元格也开始出现边框。
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let separator = UILabel(frame: CGRect(x: 15, y: cell.frame.size.height - 1, width: cell.frame.size.width, height: 1))
if indexPath.section == 0 {
if items.count == 4 {
if indexPath.row == 0 {
separator.backgroundColor = UIColor.lightGray()
cell.contentView.addSubview(separator)
}else {
separator.removeFromSuperview()
}
}else {
separator.removeFromSuperview()
}
}else {
separator.removeFromSuperview()
}
}
这行:separator.removeFromSuperview();在你的代码中什么都没有做。你没有调用想要的分离器。
更好的做法可能是在每个单元格中添加分隔符,并通过使用UITableView cellForRow func来隐藏它。