tableView.cellForRowAtIndexPath返回nil,单元格太多(swift) [英] tableView.cellForRowAtIndexPath returns nil with too many cells (swift)

查看:882
本文介绍了tableView.cellForRowAtIndexPath返回nil,单元格太多(swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个最奇怪的事情;

So I have the weirdest thing;

我循环一个tableView以迭代所有单元格。它可以在少于5个细胞的情况下正常工作,但对于更多细胞而言,意外发现为零会崩溃。这是代码:

I am looping a tableView in order to iterate over all cells. It works fine with less than 5 cells, but crashes with "unexpectedly found nil" for more cells. Here's the code:

    for section in 0..<tableView.numberOfSections {
      for row in 0..<tableView.numberofRowsInSection(section) {
        let indexPath = NSIndexPath(forRow: row, inSection: section)
        let cell = tableView?.cellForRowAtIndexPath(indexPath) as? MenuItemTableViewCell

        // extract cell properties

最后一行是给出错误。

有什么想法吗?

推荐答案

因为细胞是重用,cellForRowAtIndexPath仅在给定indexPath的单元格当前可见时才为您提供单元格。它由可选值表示。如果你想防止崩溃,你应该使用if

Because cells are reused, cellForRowAtIndexPath will give you cell only if cell for given indexPath is currently visible. It is indicated by the optional value. If you want to prevent from crash, you should use if let

if let cell = tableView?.cellForRowAtIndexPath(indexPath) as? MenuItemTableViewCell {
     // Do something with cell
}

如果你想要从单元格更新值,您的单元格应更新dataSource项目。例如,你可以为那个创建委托

If you want to update values from cell, your cells should update the dataSource items. For example you can create delegate for that

protocol UITableViewCellUpdateDelegate {
    func cellDidChangeValue(cell: UITableViewCell)
}

将代理添加到您的单元格,并假设我们在此单元格中有一个textField。我们为EditingDidChange事件添加了 didCHangeTextFieldValue:的目标,因此每次用户在其中键入somethink时都会调用它。当他这样做时,我们调用委托函数。

Add delegate to your cell and suppose we have a textField in this cell. We add target for the didCHangeTextFieldValue: for EditingDidChange event so it is called every time the user types somethink in it. And when he do, we call the delegate function.

class MyCell: UITableViewCell {
    @IBOutlet var textField: UITextField!

    var delegate: UITableViewCellUpdateDelegate?

    override func awakeFromNib() {
        textField.addTarget(self, action: Selector("didCHangeTextFieldValue:"), forControlEvents: UIControlEvents.EditingChanged)
    }

    @IBAction func didCHangeTextFieldValue(sender: AnyObject?) {
        self.delegate?.cellDidChangeValue(cell)
    }
}

然后在 cellForRowAtIndexPath 中添加代理

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier", forIndexPath: indexPath)
    cell.delegate = self

    return cell
}

最后我们实现了委托方法:

And finally we implement the delegate method:

func cellDidChangeValue(cell: UITableViewCell) {

    guard let indexPath = self.tableView.indexPathForCell(cell) else {
        return
    }

    /// Update data source - we have cell and its indexPath

}

希望有帮助

这篇关于tableView.cellForRowAtIndexPath返回nil,单元格太多(swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆