Reranging a uitableview
I ran into the same problem as desciped in: Crashing while trying to move U开发者_开发技巧ITableView rows! All I want to do is on specific reranging operations insert a new cell or delete the selected one.
Sometimes I get:
********* Assertion failure in -[_UITableViewUpdateSupport _setupAnimationForReorderingRow], /SourceCache/UIKit/UIKit-963.10/UITableViewSupport.m:295 Attempt to create two animations for cell exception
and when I want to reload my data by calling [self.tableView reloadData]
I get:
********** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
Is there anybody out there who knows how to handle this problem?
Hallo again!
Thanks for all your help! I actually found a solution for my problem!
I don't care about animating the tableview while editing anymore! I finally found a solution in this post: How to stop UITableView moveRowAtIndexPath from leaving blank rows upon reordering
Tom Saxton said:
// APPLE_BUG: if you move a cell to a row that's off-screen (because the destination // has been modified), the bogus cell gets created and eventually will cause a crash
So my code looks like this:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if( fromIndexPath == toIndexPath ) {
return;
}
if (toIndexPath.row == [self.data count] -1) { //element has been dragged to the bottom
self.data removeObjectAtIndex:fromIndexPath.row];
[self performSelector:@selector(delayedReloadData:) withObject:tableView afterDelay:0];
}
else { //Normal reoder operation
[self.data removeObjectAtIndex:fromIndexPath.row];
[self.data insertObject:element atIndex:toIndexPath.row];
}
}
- (void)delayedReloadData:(UITableView *)tableView {
//Assert(tableView == self.tableView);
[self.tableView reloadData];
}
I also had a mem leak in my initData method, which I solved like this:
-(void) initData {
if (!self.data) {
self.data = [[NSMutableArray alloc] init];
}
else {
[self.data removeAllObjects];
}
[self.data addObjectsFromArray:[self.fetchedResultsController fetchedObjects]];
}
Thanks for all your help!
Nikolay
You still have a mem leak in initData. I assume you defined your data as "@property (nonatomic, retain) NSMutableArray *data". So when you call alloc/init the retain count is 1, and then you assign to self.data which does a retain, giving it a count of 2. You need to do:
NSMutableArray *ary = [[NSMutableArray alloc] init];
self.data = ary;
[ary release];
精彩评论