簡體   English   中英

盡管滾動到tableView的頂部,tableView.cellForRow(at:IndexPath(row:0,section:0))返回nil

[英]tableView.cellForRow(at: IndexPath(row:0, section:0)) returns nil in spite of scrolling to top of tableView

使用Xcode 9.2版Swift開發iOS應用程序。

當點按RightNavigationButtonAdd時,我想在TableView的開頭插入一個TableViewCell並在其中聚焦(becomeFirstResponder)TextField。

當插入的TableViewCell可見時,它將正常工作。 但是,當插入的TableViewCell不可見時,由於cellForRow返回nil發生了錯誤。

你能告訴我如何解決這個問題嗎?

@objc func RightNavigationButtonAdd(_ sender: UIBarButtonItem) {
    // array is ["aaa", "bbb", "ccc", "ddd", ...]
    array.insert("", at: 0)
    // ttableView is @IBOutlet var ttableView: UITableView!
    ttableView.insertRows(at: [IndexPath(row:0, section:0)], with: .top)
    // Scroll to top of tableView
    UIView.animate(withDuration: 0.3, animations: {
        self.ttableView.scrollToRow(at: IndexPath(row:0, section:0), at: .top, animated: true)
    }, completion: { finished in
        // when inserted cell is not visible, error occured:
        // "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
        let cell = self.ttableView.cellForRow(at: IndexPath(row:0, section:0)) as! TableViewCell
        cell.textField.becomeFirstResponder()
        cell.textField.placeholder = "input anything ..."
    })
}

更新資料

我不知道為什么,但是如果滾動動畫:“ false”,則效果很好!

@objc func RightNavigationButtonAdd(_ sender: UIBarButtonItem) {
    // array is ["aaa", "bbb", "ccc", "ddd", ...]
    array.insert("", at: 0)
    // ttableView is @IBOutlet var ttableView: UITableView!
    ttableView.insertRows(at: [IndexPath(row:0, section:0)], with: .top)
    // Scroll to top of tableView
    UIView.animate(withDuration: 0.3, animations: {
        self.ttableView.scrollToRow(at: IndexPath(row:0, section:0), at: .top, animated: false)
    }, completion: { finished in
        // when inserted cell is not visible, error occured:
        // "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
        let cell = self.ttableView.cellForRow(at: IndexPath(row:0, section:0)) as! TableViewCell
        cell.textField.becomeFirstResponder()
        cell.textField.placeholder = "input anything ..."
    })
}

如果要獲得可見的單元格,則應采用這種方式

let cell =  self.ttableView.visibleCells[0]

然后,您可以選擇是否存在單元格,或者檢查self.ttableView.visibleCells的大小。

所以最后

@objc func RightNavigationButtonAdd(_ sender: UIBarButtonItem) {
// array is ["aaa", "bbb", "ccc", "ddd", ...]
array.insert("", at: 0)
// ttableView is @IBOutlet var ttableView: UITableView!
ttableView.insertRows(at: [IndexPath(row:0, section:0)], with: .top)
// Scroll to top of tableView
UIView.animate(withDuration: 0.3, animations: {
    self.ttableView.scrollToRow(at: IndexPath(row:0, section:0), at: .top, animated: true)
}, completion: { finished in
    if self.ttableView.visibleCells.count > 0 {
    let cell = self.ttableView.visibleCells[0] as! TableViewCell
    cell.textField.becomeFirstResponder()
    cell.textField.placeholder = "input anything ..."
    }
})

}

暫無
暫無

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

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