swift reloadRows方法 坑記

起因:

自定義了2個cell 

其中一個cell 點擊後調整佈局,我使用的是reloadRows 然後把indexpath放到數組裏面

發現抖動一下,開始沒當回事

後來發現 創建tbv後第一次點擊,佈局變化都是沒有效果的,只有2+次以後纔有效果,這就困擾了我

後來調試發現init方法執行了2次

也就是dequeue方法調用了2次

複用池裏面沒有。。。。這個,所以又創建了,我第一次點擊就又創建了一次view所有佈局沒有變化,以後 池子裏就有了

but 這不是我想要的效果

 

--------

 

後來直接不用局部刷新,換成全局刷新,就解決這個問題了。。。

 

原因:

reloadRows 方法 會導致複用池發生變化,cell混亂抖動....

 

代碼:

  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        makeUI()  // 執行2次
    }
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 && indexPath.row == 1 {
            let cell  = tableview.dequeueReusableCell(withIdentifier: ImportMnemonicRouterCellInd) as! ImportMnemonicRouterCell  // 失去了本意效果
            let arr = dataArray?[indexPath.section]
            cell.model = arr?[indexPath.row]
            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            return cell

        }else{
            let cell  = tableview.dequeueReusableCell(withIdentifier: ImportCreateRouterCellInd) as! ImportCreateRouterCell
            let arr = dataArray?[indexPath.section]
            cell.model = arr?[indexPath.row]
            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            return cell

        }

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章