2013-07-18 67 views
0

我遇到了在編輯模式下重新安排單元格在tableView中的問題。向下移動單元格可以正常工作,但是會將單元格向上移動,從而造成訂單上的嚴重破壞。有時會回到正常位置,有時會選擇一個隨機點結束。具有諷刺意味的是,Core數據模型和訂單每次都會正確結束。'向上移動單元格'在tableView中未正確更新查看

我已經通過STO搜索,並且還沒有找到解決方案。

- 沒有應用手勢控制,我試過禁用它們。

-Try [self setSuspendAutomaticTrackingOfChangesInManagedObjectContext:NO];

-Try [self.tableView.canCancelContentTouches = NO];

-NSFetchControls /與BOOL繞過代表

我使用移動的細胞代碼基本上是一樣的:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
[super setEditing:editing animated:animated]; 
[_tableView setEditing:editing animated:animated]; 
if(editing) { 
    NSInteger rowsInSection = [self tableView:_tableView numberOfRowsInSection:0]; 
    // Update the position of all items 
    for (NSInteger i=0; i<rowsInSection; i++) { 
     NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
     SomeManagedObject *curObj = [_fetchedResultsController objectAtIndexPath:curIndexPath]; 
     NSNumber *newPosition = [NSNumber numberWithInteger:i]; 
     if (![curObj.displayOrder isEqualToNumber:newPosition]) { 
     curObj.displayOrder = newPosition; 
     } 
    } 
} 
} 

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
    NSInteger moveDirection = 1; 
    NSIndexPath *lowerIndexPath = toIndexPath; 
    NSIndexPath *higherIndexPath = fromIndexPath; 
    if (fromIndexPath.row < toIndexPath.row) { 
    // Move items one position upwards 
    moveDirection = -1; 
    lowerIndexPath = fromIndexPath; 
    higherIndexPath = toIndexPath; 
} 

// Move all items between fromIndexPath and toIndexPath upwards or downwards by one position 
for (NSInteger i=lowerIndexPath.row; i<=higherIndexPath.row; i++) { 
    NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:i inSection:fromIndexPath.section]; 
    SomeManagedObject *curObj = [_fetchedResultsController objectAtIndexPath:curIndexPath]; 
    NSNumber *newPosition = [NSNumber numberWithInteger:i+moveDirection]; 
    curObj.displayOrder = newPosition; 
} 

SomeManagedObject *movedObj = [_fetchedResultsController objectAtIndexPath:fromIndexPath]; 
movedObj.displayOrder = [NSNumber numberWithInteger:toIndexPath.row]; 
NSError *error; 

if (![_fetchedResultsController.managedObjectContext save:&error]) { 
    NSLog(@"Could not save context: %@", error); 
} else { 
    [self.tableview reloadData]; 
} 
} 

希望有人能回答這個問題,因爲回答問題STO人太棒了! :) 提前致謝!!!

-UPDATE-

我發現,當我移動單元格向上的方法保持引用相同的對象,並將其更新到新秩序,而不是更新與新訂單的下一個對象。

即。 Move cell up - Results =(Object1.order = 1,Object1.order = 2,Object1.order = 3)

ie。移動單元向下 - 結果=(Object1.order = 1,Object2.order = 2,Object3.order = 0)

回答

0

變化

for (NSInteger i=lowerIndexPath.row; i<=higherIndexPath.row; i++) 

for (NSInteger i=fromIndexPath.row; i<=toIndexPath.row; i -= moveDirection) 
+0

感謝您的答覆,但它並沒有區別對我來說,也許它會爲別人。 –