Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

iphone - How to deal with non-visible rows during row deletion. (UITableViews)

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];

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...