我正在使用主要来自cimgf的代码来更新通过pushViewController显示的UITableViewController表中的行的顺序。示例代码效果很好,但在我的项目中,当我移动行时,它们会消失,搞砸或卡住(例如:http://sites.google.com/site/basicmjc/home/uiviewcorrupt.png)。
以下是一些相关代码:
谁能告诉我发生了什么?
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSMutableArray *things = [[self.fetchedResultsController fetchedObjects] mutableCopy];
// Grab the item we're moving.
NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];
// Remove the object we're moving from the array.
[things removeObject:thing];
// Now re-insert it at the destination.
[things insertObject:thing atIndex:[destinationIndexPath row]];
// All of the objects are now in their correct order. Update each
// object's displayOrder field by iterating through the array.
int i = 0;
for (NSManagedObject *mo in things)
{
NSLog(@"%i - %@", i, [mo valueForKey:@"acctAddr"]);
[mo setValue:[NSNumber numberWithInt:i++] forKey:@"sortOrder"];
}
[things release], things = nil;
// Save
NSError *error = nil;
if (![self.managedObjectContext save:&error])
{
NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
NSLog(@"%@\n\nDetails: %@", msg, details);
}
// re-do the fetch so that the underlying cache of objects will be sorted
// correctly
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSString *tempStr;
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
//cell.textLabel.text = [[managedObject valueForKey:@"acctAddr"] description];
tempStr = [[managedObject valueForKey:@"acctFriendlyName"] description];
if ([tempStr length] < 1) {
tempStr = [[managedObject valueForKey:@"acctAddr"] description];
}
else cell.detailTextLabel.text = [[managedObject valueForKey:@"acctAddr"] description];
cell.textLabel.text = tempStr;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}