2011-01-25 76 views
0

我想在iOS應用程序中重新排列UITableView。
收穫了不少的StackOverflow問題後,我用這個:最後一條解決重新排序UITableView支持核心數據拼圖

- (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

您已經將保存消息發送到managedObjectContext,這很好,if (! [self.fetchedResultsController.managedObjectContext save:&error])

要在表格視圖中查看更新的結果,在此方法結束時調用[tableView reloadData];可能就足夠了。

+0

這似乎是它更新了各個單元的顯示數據,但沒有秩序,有沒有人設法讓這種方法要奏效? – 2012-05-01 20:57:37

0

您還應該檢查from和index路徑是否相等,如果它們會崩潰!

if([toIndexPath isEqual:fromIndexPath]) return; 
0

//上述答案是正確的。這裏將是另一個PROPERLY-工作例用我自己的客體名字

編譯馬克 - 重新排序

  • (BOOL)的tableView:(UITableView的*)的tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 回報indexPath.section == 0 & & indexPath.row < self.list.sortedTasks.count; }

  • (無效)的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]; 任務* destinationTask = [self.list.sortedTasks objectAtIndex:destinationIndexPath.row];

    NSArray * tempList = self.list.sortedTasks;

    if(fromIndex == toIndex){ return; }

    //將活動任務移動到目標任務的位置。 movingTask.order = destinationTask.order; NSLog(@「將%@轉移到%@」,movingTask.details,movingTask。訂購);

    NSUInteger開始,結束; int order;

    if(fromIndex < toIndex){ // move is down,need to shift up order = -1; start = fromIndex + 1; end = toIndex; }否則{//的fromIndex> toIndex //此舉是起來,需要進行降檔 順序= 1; start = toIndex; end = fromIndex - 1; }

    爲( NSUInteger I =開始;我< =端;我++){ 任務* otherObject = [tempList objectAtIndex:ⅰ]; 的NSNumber * oldPriority = otherObject.order; INT TMP = [oldPriority的intValue] +順序; 的NSNumber *的newPriority = [NSNumber的numberWithInt:TMP]; otherObject.order =的newPriority;

    NSLog(@"moving %@ to %@", otherObject.details, otherObject.order); 
    

    }

    [self.managedObjectContext保存:無];

}