“尝试从第1部分删除行,但在更新前只有1个部分” [英] "Attempt to delete row from section 1, but there are only 1 sections before update"

查看:1191
本文介绍了“尝试从第1部分删除行,但在更新前只有1个部分”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除不符合for循环条件的行。但是,我得到的错误是:'尝试从第1部分删除第0行,但在更新之前只有1个部分。我以前从未见过这个,也不确定为什么我会得到它。

I'm trying to delete the rows that don't meet the criteria in the for loop. However, I'm getting and error that says: 'attempt to delete row 0 from section 1, but there are only 1 sections before the update." I'v never seen this before and not sure why I am getting it.

我的代码:

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

    let cell = theTableView.dequeueReusableCellWithIdentifier("MyCell") as! TableViewCell
    cell.titleLabel.text = titles[indexPath.row]
    cell.priceLabel.text = prices[indexPath.row]
    cell.descLabel.text = descs[indexPath.row]
    cell.itemImage.image = itemImages[indexPath.row]
    cell.userNumber = phoneNumbers[indexPath.row] as! String
    cell.timeLabel.text = datesHours[indexPath.row]! + "hr"
    cell.distanceLabel.text = String(locations[indexPath.row]!) + "mi"
    cell.viewController = self




    self.theTableView.beginUpdates()

    for (index, number) in self.locations {
        if number <= 5 {
            let indexPath = NSIndexPath(forRow: number, inSection: 1)
            self.theTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

        }
    }
    self.theTableView.endUpdates()


推荐答案

好像你告诉表格会有更多的行,而不是你想要显示的行,然后在事实上。

It seems like you are telling the table there will be more rows than you actually want to display and then deleting them after the fact.

相反,你应该检查数组中的元素是否符合(或之前)的标准 numberOfRowsInSection 并将它们放入一个实际显示的不同数组中,以便表知道实际显示的行数。然后在 cellForRowAtIndexPath 中使用具有实际显示数据的新创建数组。

Instead you should be checking if the elements in the array meet the criteria in (or before) numberOfRowsInSection and putting them into a different array that will actually be displayed so that the table knows how many rows will actually be displayed. Then in cellForRowAtIndexPath just use the newly create array that has the data that will actually be displayed.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.

        self.displayArray = []
        for (index, number) in self.locations {
            if number <= 5 {
               self.displayArray.Add(self.locations[index])
            }
        }
        return self.displayArray.Count
    }

我假设您的错误与您尝试在首次尝试创建表的方法中更新表有关。您正在尝试更新尚未完全创建的内容。

I'm assuming your error has something to do with you trying to update the table in a method that is first trying to create it. You're trying to update something that hasn't fully been created.

这篇关于“尝试从第1部分删除行,但在更新前只有1个部分”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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