在下面的代码中,是否有任何错误,在tableViewCell的底边添加了特定行的边框。

问题描述 投票:1回答:1

下面的代码是工作的,但当我多次滚动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()
    }
}
ios swift iphone xcode uitableview
1个回答
0
投票

这行:separator.removeFromSuperview();在你的代码中什么都没有做。你没有调用想要的分离器。

更好的做法可能是在每个单元格中添加分隔符,并通过使用UITableView cellForRow func来隐藏它。

© www.soinside.com 2019 - 2024. All rights reserved.