【问题标题】:UITableView row separator cut off when updating cell更新单元格时 UITableView 行分隔符被切断
【发布时间】:2015-12-01 11:08:15
【问题描述】:

我有一个分组的 UITableView,我在第二部分中显示了一些汽车数据。我使用SDWebImage 从网络服务器加载图像。在其回调中,我调整图片大小并更新我的图像视图。

但是,一旦我更新我的图像视图,UITableView 分隔符就会被切断。 出于说明目的,我给了相应的元素背景颜色 当图像尚未加载时,它看起来像这样:

图片更新后是这样的

请注意,即使没有子视图(UIImageView)隐藏行分隔符,UITableView 单元格之间的行分隔符也会以某种方式被切断。

根据UITableView 部分,行高会有所不同,因此我覆盖了heightForRowAtIndexPath UITableView 委托

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 {

            return GFFloat(44)
        }
        else {

            return CGFloat(220)
        }

    }

谁能告诉我为什么分隔符消失了,我该如何解决这个问题?当我更新图像时,我已经阅读了有关重新加载下一个 UITableViewCell 的信息,但是由于我显示了很多汽车,所以当我尝试这个时,我的应用程序崩溃了。

【问题讨论】:

    标签: ios objective-c iphone swift uitableview


    【解决方案1】:

    只需添加以下方法即可解决分隔符问题。

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            if cell.respondsToSelector("setSeparatorInset:") {
                cell.separatorInset = UIEdgeInsetsZero
            }
            if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
                cell.preservesSuperviewLayoutMargins = false
            }
            if cell.respondsToSelector("setLayoutMargins:") {
                cell.layoutMargins = UIEdgeInsetsZero
            }
        }
    

    【讨论】:

    • 太棒了。你能解释一下为什么会发生截止吗?我不太明白为什么它现在有效。
    【解决方案2】:

    Swift 5 的解决方案

    将此添加到您的控制器:

    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = UIEdgeInsets.zero
        }
        if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false
        }
        if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
            cell.layoutMargins = UIEdgeInsets.zero
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多