我正在尝试在iOS应用程序中重新排序UITableView 在收获了很多StackOverFlow问题后,我使用了这个:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
self.userDrivenDataModelChange = YES;
NSUInteger fromIndex = fromIndexPath.row;
NSUInteger toIndex = toIndexPath.row;
NSManagedObject *affectedObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];
[affectedObject setValue: [NSNumber numberWithInt:toIndex] forKey:@"priority"];
NSUInteger start, end;
int delta;
if (fromIndex < toIndex) {
// move was down, need to shift up
delta = -1;
start = fromIndex + 1;
end = toIndex;
} else { // fromIndex > toIndex
// move was up, need to shift down
delta = 1;
start = toIndex;
end = fromIndex - 1;
}
for (NSUInteger i = start; i <= end; i++) {
NSManagedObject *otherObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:i];
NSNumber *oldPriority = (NSNumber *)[otherObject valueForKey:@"priority"];
int tmp = [oldPriority intValue] + delta;
NSNumber *newPriority = [NSNumber numberWithInt:tmp];
[otherObject setValue: [NSNumber numberWithInt:[newPriority intValue]] forKey:@"priority"];
}
NSError *error = nil;
if (! [self.fetchedResultsController.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Should I call something here?
self.userDrivenDataModelChange = NO;
}
将保存消息发送到上下文对象后,我该怎么办?获取控制器或表视图上的某些方法?因为它不能以当前的形状工作。
我应该首先将保存发送到上下文对象吗?
答案 0 :(得分:0)
您已经向managedObjectContext发送了保存消息,这很好,if (! [self.fetchedResultsController.managedObjectContext save:&error])
。
要在表格视图中查看更新的结果,在此方法结束时调用[tableView reloadData];
可能就足够了。
答案 1 :(得分:0)
您还应该检查索引路径是否相等,如果它们都崩溃了!
if([toIndexPath isEqual:fromIndexPath]) return;
答案 2 :(得分:0)
//上面的答案是正确的。我的自己的对象名称将是另一个正常工作的例子
(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.section == 0&amp;&amp; indexPath.row&lt; self.list.sortedTasks.count; }
(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSInteger fromIndex = sourceIndexPath.row; NSInteger toIndex = destinationIndexPath.row;
Task * movingTask = [self.list.sortedTasks objectAtIndex:sourceIndexPath.row]; Task * destinationTask = [self.list.sortedTasks objectAtIndex:destinationIndexPath.row];
NSArray * tempList = self.list.sortedTasks;
if(fromIndex == toIndex){ 返回; }
//将活动任务移动到目标任务的位置。 movingTask.order = destinationTask.order; NSLog(@&#34;将%@移动到%@&#34;,movingTask.details,movingTask.order);
NSUInteger开始,结束; int order;
if(fromIndex&lt; toIndex){ //移动了,需要向上移动 order = -1; start = fromIndex + 1; end = toIndex; } else {// fromIndex&gt; toIndex //移动了,需要向下移动 order = 1; start = toIndex; end = fromIndex - 1; }
for( NSUInteger i =开始;我&lt; =结束;我++){ Task * otherObject = [tempList objectAtIndex:i]; NSNumber * oldPriority = otherObject.order; int tmp = [oldPriority intValue] + order; NSNumber * newPriority = [NSNumber numberWithInt:tmp]; otherObject.order = newPriority;
NSLog(@"moving %@ to %@", otherObject.details, otherObject.order);
}
[self.managedObjectContext save:nil];
}