ios - tableView.cellForRow(at : IndexPath(row:0, section:0)) 返回 nil 尽管滚动到 tableView 顶部

标签 ios swift xcode

使用 Xcode 9.2 版、Swift 开发 iOS 应用程序。

当点击 RightNavigationButtonAdd 时,我想在 TableView 的开头插入一个 TableViewCell 并将焦点(成为FirstResponder)其中的 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 ..."
    }
})

}

关于ios - tableView.cellForRow(at : IndexPath(row:0, section:0)) 返回 nil 尽管滚动到 tableView 顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48464519/

相关文章:

ios - 如何访问具有多个元素的字典键中的元素?

ios - 以编程方式创建更高分辨率的 PNG 图像

ios - 在 UIView 周围绘制 CALayer 边框

ios - 从 UITableViewCell 隐藏删除按钮

ios - Swift - 单击按钮更改 UIPageViewController View

ios - 从 Swift 3 中的锁屏清除本地通知

c++ - 在 C++ 中使用 TARGET_IPHONE_SIMULATOR 时出现链接器错误

ios - UIActivityViewController 的标记被取消后方向错误

ios - GAD_GTMStringEncoding 类在 <framework> 和 <app> 中都实现了。将使用两者之一。哪个是未定义的

objective-c - ABAddressBookGetPersonCount 在 iOS 中返回 -1