首页 > 解决方案 > UITableView 分隔符在以编程方式设置时消失

问题描述

我试图在部分中隐藏最后一个单元格和单个单元格的分隔符。我的方法 cellForRow 如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UITableViewCell.self), for: indexPath)
    
    if dataSource.count - 1 == indexPath.row {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
    } else {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    }
    
    return cell
}

它可以工作,但在某些情况下,当 else 块执行时,分隔符仍然不可见。它发生在最后一个单元格重用于中间单元格时,但我不明白为什么不应用更新的分隔符插图。这个问题的原因可能是什么?

标签: iosuitableviewuikittableview

解决方案


推荐阅读