如何在删除行时处理不可见的行。 (UITableViews) [英] How to deal with non-visible rows during row deletion. (UITableViews)

查看:88
本文介绍了如何在删除行时处理不可见的行。 (UITableViews)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行删除表的几行。事实上,在这种情况下,所有可见的单元格事实上都被删除。



这会导致可见框架下面的单元格在动画后变得可见,因为它上面的所有单元格



这里是问题,此单元格也被删除。所以我得到以下错误,我想,因为tableview要我删除不在屏幕上的单元格。 (但在动画期间/之后会显示在屏幕上)。

  2009-06-15 17:44:30.114 HSS [18175 :20b] ***由于未捕获异常NSInternalInconsistencyException而终止应用程序,原因:无效的更新:第4节中的行数不正确。更新后现有段中包含的行数(0)必须等于更新前该部分中包含的行数(1),加上或减去从该部分插入或删除的行数(插入0,删除0)'
2009-06-15 17:44:30.117 HSS [18175:20b]堆栈:(
807902715,
2429263419,
807986683,
811271572,
815059595,
815007323,
97367 ,
96328,
96282,
810768858,
807687328,
807683624,
839142449,
839142646,
814752238,
10968,
10822

这种情况?



导致异常的代码如下。注意,我实际上在另一个方法中创建一个名为filteredTableGroups的新数组。这是更新的模型。 allTableGroups是每个单元控制器。每个单元格控制器包含一个字典,用于填充其单元格数据。我使用一个键,filteredDataSet,以确定单元格是否应该保留在过滤表中。在删除tableview单元格期间,我交换tableGroups以指向更新的模型。 (我创建的代码类似于Matt Gallagher和Craig Hockenberry使用Cell Controller控制单个单元格的解决方案)

   - )collapseVisableCells {

NSMutableArray * cellsToRemove = [NSMutableArray array];

(self.allTableGroups中的NSArray *部分){

for(ScheduleCellController * cellController in sections){

NSString * shouldDisplayString = *)[[cellController model] objectForKey:@filteredDataSet];

BOOL shouldDisplay = [shouldDisplayString boolValue];

if(!shouldDisplay){

UITableViewCell * theCell = [cellController myCell];

NSIndexPath * cellPath = [self.tableView indexPathForCell:theCell];
NSLog([cellPath description]);

if(cellPath!= nil)
[cellsToRemove addObject:cellPath];


}
}

}



[self.tableView beginUpdates];
tableGroups = self.filteredTableGgroups;
[self.tableView deleteRowsAtIndexPaths:cellsToRemove withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];

}

解决方案<

  NSMutableIndexSet * 

indexes = [NSMutableIndexSet indexSet];

每次从与tableView的特定部分相关的模型数组中删除一个对象时,数组计数为零,在这种情况下,向索引添加表示段的索引:

  [array removeObjectAtIndex:indexPath.row] ; 
if(![array count])
[indexes addIndex:indexPath.section];

确定与要删除的行相关的所有indexPath,然后更新tableView,如下所示:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

让我知道这是否适合你。


I am performing a deletion of several rows of a table. In fact, on this occasion all visible cells are in fact deleted.

This causes the cell immediately below the visible frame to become visible after the animation, since all cells above it are now gone.

Here is the issue, this cell is also deleted. So I get the following error, I assume, because the tableview wants me to delete a cell that isn't on screen. (but WILL be on screen during/after the animation).

2009-06-15 17:44:30.114 HSS[18175:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 4.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
2009-06-15 17:44:30.117 HSS[18175:20b] Stack: (
    807902715,
    2429263419,
    807986683,
    811271572,
    815059595,
    815007323,
    97367,
    96328,
    96282,
    810768858,
    807687328,
    807683624,
    839142449,
    839142646,
    814752238,
    10968,
    10822
)

So what do I do in this situation?

Code that causes exception follows. Note that I actually create a new array called "filteredTableGroups" in another method. This is the updated model. "allTableGroups" is every Cell Controller. Each cell controller contains a dictionary used to populate it's cells data. I use a key, "filteredDataSet", to determine whether a cell should remain in the filtered table. During the tableview cell deletion, I swap tableGroups to point to the updated model. (I am created my code similar to Matt Gallagher and Craig Hockenberry's solution of using Cell Controller's to control individual cells)

- (void)collapseVisableCells{

NSMutableArray *cellsToRemove = [NSMutableArray array];

for(NSArray *sections in self.allTableGroups){

    for(ScheduleCellController *cellController in sections){

        NSString *shouldDisplayString = (NSString*)[[cellController model] objectForKey:@"filteredDataSet"];

        BOOL shouldDisplay = [shouldDisplayString boolValue];

        if(!shouldDisplay){

            UITableViewCell *theCell = [cellController myCell];

            NSIndexPath *cellPath = [self.tableView indexPathForCell:theCell];
            NSLog([cellPath description]);

            if(cellPath!=nil)
                [cellsToRemove addObject:cellPath];


        }
    }

}



[self.tableView beginUpdates];
tableGroups = self.filteredTableGroups;
[self.tableView deleteRowsAtIndexPaths:cellsToRemove withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];

}

解决方案

I am not sure I have understood correctly what you mean, but you can try the following.

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];

Each time you delete an object from your model array related to a specific section of the tableView, check if the array count is zero, in which case add an index representing the section to indexes:

[array removeObjectAtIndex:indexPath.row];
if(![array count])
    [indexes addIndex: indexPath.section];

Determine all of the indexPaths related to the rows to be deleted, then update the tableView as follows:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

Let me know if this works for you.

这篇关于如何在删除行时处理不可见的行。 (UITableViews)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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