Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 解决重新排序UITableView与核心数据难题的最后一步_Objective C_Cocoa Touch_Ios_Uitableview - Fatal编程技术网

Objective c 解决重新排序UITableView与核心数据难题的最后一步

Objective c 解决重新排序UITableView与核心数据难题的最后一步,objective-c,cocoa-touch,ios,uitableview,Objective C,Cocoa Touch,Ios,Uitableview,我正在尝试在iOS应用程序中重新排序UITableView。 在收集了大量StackOverFlow问题后,我使用了以下方法: - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { self.userDrivenDataModelChange = YES; NSUInte

我正在尝试在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;
}
-(void)tableView:(UITableView*)tableView移动rowatindexpath:(nsindepath*)从indepath到indepath:(nsindepath*)到indepath{
self.userdrivendamodelchange=是;
NSU整数fromIndex=fromIndexath.row;
NSU整数toIndex=toIndexPath.row;
NSManagedObject*affectedObject=[self.fetchedResultsController.fetchedObject对象索引:fromIndex];
[affectedObject设置值:[NSNumber numberWithInt:toIndex]forKey:@“priority”];
开始,结束;
内特三角洲;
如果(从索引<到索引){
//下移,需要上移
delta=-1;
开始=从索引+1开始;
结束=toIndex;
}else{//fromIndex>toIndex
//移动是向上的,需要向下移动
δ=1;
开始=指数;
结束=从索引-1开始;
}

对于(i=start;i您已经将保存消息发送到managedObjectContext,这很好,
如果(![self.fetchedResultsController.managedObjectContext save:&error])


要在表视图中查看更新的结果,在该方法结束时调用
[tableView reloadData];
可能就足够了。

您还应该检查从索引路径和到索引路径是否相等,如果相等,它将崩溃

if([toIndexPath isEqual:fromIndexPath]) return;

//上面的答案是正确的。下面是另一个使用我自己的对象名正常工作的示例

pragma标记-重新排序
  • (BOOL)tableView:(UITableView*)tableView可以移动rowatinexpath:(nsindepath*)indepath{ 返回indexPath.section==0&&indexPath.row
  • (void)tableView:(UITableView*)tableView MoveRowatineXpath:(NSIndexPath*)sourceIndexPath到IndeXpath:(NSIndexPath*)destinationIndexPath{

    NSInteger fromIndex=sourceindexath.row; NSInteger toIndex=destinationIndepath.row

    Task*movingTask=[self.list.sortedTasks objectAtIndex:sourceindexath.row]; Task*destinationTask=[self.list.sortedTasks objectAtIndex:destinationIndepath.row]

    NSArray*templast=self.list.sortedTasks

    如果(从索引==到索引){ 回来 }

    //将活动任务移动到目标任务的位置。 movingTask.order=destinationTask.order; NSLog(@“移动%@到%@”、movingTask.details、movingTask.order)

    开始,结束; 整数阶

    如果(从索引<到索引){ //下移,需要上移 顺序=-1; 开始=从索引+1开始; 结束=toIndex; }else{//fromIndex>toIndex //移动是向上的,需要向下移动 顺序=1; 开始=指数; 结束=从索引-1开始; }

    为了(
    NSUInteger i=start;i这似乎更新了单个单元格的显示数据,但没有更新顺序,是否有人设法使这种方法起作用?
    NSLog(@"moving %@ to %@", otherObject.details, otherObject.order);