从UITableView中删除项目后,索引超出范围 [英] Index out of range after item has been deleted from UITableView

查看:84
本文介绍了从UITableView中删除项目后,索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我找出我的错误。我有一个表视图,数据源是一个称为项的数组。从项数组中删除一项后,将调用cellForRowAt方法,并且参数indexPath.row与items.count相等。仅当行计数刚好足以使某项超出表视图的视图时,才会发生这种情况。
发生这种情况时,它将导致致命错误(索引超出范围)。在这种情况下,将使用hack,并且IndexPath.row的值将减小。

Please help me find what is my mistake. I have a table view the data source is an array called items. After one item has been deleted from the items array the cellForRowAt method called and the parameter indexPath.row is equals with items.count. This happens only, when the rows count is just enough to one item is out of the table view's view. When it happens it cause a fatal error (index out of range). A hack is used in this case and the value of the IndexPath.row be decreased.

请参见下图:

cellForRowAt的以下代码:

The following code for cellForRowAt:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell

    var i = indexPath.row
    if i >= items.count { i = items.count - 1 } //XXX: hack! Sometimes called with indexPath.row is equal with items.count :(
    cell.set(items[i])

    return cell
}

以及以下用于删除的代码:

And the following code for the delete:

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    if (editingStyle == UITableViewCellEditingStyle.delete)
    {
        items.remove(at: indexPath.row)

        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
    }
}

我认为不相关,但我使用的是iOS 11.0

更新

我尝试过,以下非常简单的代码也受到了影响:

I tried, and the following very simple code is also affected:

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
    var items = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
        cell.textLabel?.text = "\(items[indexPath.row])"

        return cell
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
    {
        if (editingStyle == UITableViewCellEditingStyle.delete)
        {
            items.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
        }
    }
}

如何复制:


  1. 向下滚动到Y就是屏幕上的最后一项


  1. 尝试删除向左滑动的Y


  1. 并遇到以下错误

推荐答案

您的删除代码看起来不错,但是您不应该在 cellForRowAt 中进行操作。改为这样做:

Your deletion code looks fine, but you should not do what you´re doing in your cellForRowAt. Do it like this instead:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
    cell.set(items[indexPath.row])

    return cell
}

您的 numberOfRowsInSection 应该是这样的:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

更新:

我们刚刚确认这是与Xcode 9 beta和iOS 11有关的错误。在Xcode 8.x上可以正常工作。 OP会为此向Apple提交 bug

这篇关于从UITableView中删除项目后,索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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