ios - 向正确的 UITableViewCell 添加边框时的问题

标签 ios swift uitableview

当试图在我的 UITableViewCells 周围设置边框时,我遇到了一个问题,即每当向下滚动 TableView 时,都会在我不希望它出现的地方添加一个边框!我需要为第一个和最后一个单元格单独设置这些边框,因为 TableView 本身有一个我不想包含在边框中的标题。我已经有一个页脚,所以我也不能使用该选项。
这是我的代码的样子:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
    
    cell.viewMain.addBorders(edges: [.left, .right], color: UIColor.black)
    
    if indexPath.row == 0 {
        cell.viewMain.addBorders(edges: [.top], color: UIColor.black)
    } else if indexPath.row == ((self.dataArray.count ?? 1) - 1){
        cell.viewMain.addBorders(edges: [.bottom], color: UIColor.black)
    }
这是我向下滚动和向后滚动时 TableView 的样子:
TableView Issues
为了保密,我不得不编辑数据,但看到问题了吗?边框应该只在顶部和底部,当 View 最初加载时,它看起来是正确的。只有当用户向下滚动或屏幕不够大而无法看到整个 TableView 时,我才会遇到此问题。

最佳答案

因为单元格被回收了,所以在添加边框之前需要去掉所有的边框

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell

    // remove all borders here        

    cell.viewMain.addBorders(edges: [.left, .right], color: UIColor.black)
    
    if indexPath.row == 0 {
        cell.viewMain.addBorders(edges: [.top], color: UIColor.black)
    } else if indexPath.row == ((self.dataArray.count ?? 1) - 1){
        cell.viewMain.addBorders(edges: [.bottom], color: UIColor.black)
    }

关于ios - 向正确的 UITableViewCell 添加边框时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62882513/

相关文章:

ios - 快速 NSPredicate 逻辑或

ios - 滑动操作不适用于节标题

ios - 使用 Alamofire 4 将照片上传到 Swift 3 中的 Django?

swift - 如何更改 TableView 中按钮的标题 (Swift)

ios - 无法确定谁可以使用内部开发安装 iOS 应用程序?

ios - TouchUpInside 按钮不工作

ios - viewForHeaderInSection 滚动时消失

ios - 如何在 Xcode 中为 tableView 更改原型(prototype)单元格的高度?

ios - Xcode (Swift) 文档说将属性设置为 YES,但仅在设置为 true 时有效?

iphone - 在 viewDidLoad 中的子类之后, [super viewDidLoad] 被调用,但方法没有被执行