ios “尝试从第 1 节中删除行,但更新前只有 1 个节”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32016708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:16:21  来源:igfitidea点击:

"Attempt to delete row from section 1, but there are only 1 sections before update"

iosswiftuitableview

提问by

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.

我正在尝试删除不符合 for 循环条件的行。但是,我收到错误消息:“尝试从第 1 部分删除第 0 行,但更新前只有 1 个部分。”我以前从未见过这种情况,也不知道为什么会得到它。

My code:

我的代码:

 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()

采纳答案by dmarsi

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.

似乎您是在告诉表将有比您实际想要显示的行更多的行,然后在事后将其删除。

Instead you should be checking if the elements in the array meet the criteria in (or before) numberOfRowsInSectionand 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 cellForRowAtIndexPathjust use the newly create array that has the data that will actually be displayed.

相反,您应该检查数组中的元素是否满足(或之前)的条件numberOfRowsInSection,并将它们放入实际显示的不同数组中,以便表格知道实际显示的行数。然后cellForRowAtIndexPath只需使用新创建的数组,该数组具有将实际显示的数据。

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.

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