簡體   English   中英

更新單元格時 UITableView 行分隔符被切斷

[英]UITableView row separator cut off when updating cell

我有一個分組的 UITableView,其中我在第二部分顯示了一些汽車數據。 我使用SDWebImage從網絡服務器加載圖像。 在它的回調中,我調整了圖片的大小並更新了我的圖像視圖。

但是,一旦我更新我的圖像視圖, UITableView 分隔符就會被切斷。 為了說明的目的,我給了各自的元素背景顏色當圖像尚未加載時,它看起來像這樣:

行分隔符可見

更新圖像后,它看起來像這樣

在此處輸入圖片說明

請注意,行分隔符在 UI​​TableView 單元格之間以某種方式被切斷,即使沒有子視圖( UIImageView )隱藏它。

根據UITableView部分,行高會有所不同,所以我覆蓋了heightForRowAtIndexPath UITableView 委托

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

            return GFFloat(44)
        }
        else {

            return CGFloat(220)
        }

    }

有人能告訴我為什么分隔符消失了,我該如何解決這個問題? 我已經閱讀了有關在更新圖像時重新加載下一個UITableViewCell的信息,但是由於我顯示了很多汽車,因此當我嘗試這樣做時,我的應用程序崩潰了。

只需添加以下方法即可解決分隔符問題。

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
        }
    }

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM