2012-11-02 105 views
1

我如何重新排序細胞使用FRC CoreData工作時,我看到很多帖子這表明使用順序屬性和更新相應這個來了,一個這樣的代碼是在這裏下面重新排序細胞使用取ResultsController

插入新對象我必須設置顯示順序,並增加它根據

這裏是它的代碼

- (void)insertNewObject 
{ 
    Test *test = [NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.managedObjectContext]; 

NSManagedObject *lastObject = [self.controller.fetchedObjects lastObject]; 

float lastObjectDisplayOrder = [[lastObject valueForKey:@"displayOrder"] floatValue]; 

[test setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:@"displayOrder"]; 

} 



- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
     toIndexPath:(NSIndexPath *)destinationIndexPath; 
{ 
    NSMutableArray *things = [[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) 
    { 
    [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"]; 
    } 

    [things release], things = nil; 

// [managedObjectContext save:nil]; 
    NSError *error = nil; 

    if (![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 (![fetchedResultsController performFetch:&error]) 
    { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
} 

但是假設我有100個項目,我從中刪除任何一個項目,然後我必須重新計算displayOrder,我認爲這是不可行的。是否有任何替代辦法做到這一點的過程

問候 蘭吉特

+0

有人可以幫我在這 – Ranjit

回答

3

爲什麼你認爲你需要重新計算索引號?

這些項目按照您應用的排序順序排列。

如果您提供升序排序,並提供一個數字,那麼您將得到正確的排序。您不需要的是您用於訂購商品的數量與數組中商品的索引之間的直接對應關係。例如:如果你的訂單號是

1 2 3 4 

然後,當你把它們抓下來,並整理他們,他們都會出現在這個順序。但是,如果訂購號碼不同,它們仍將以相同的順序出現。

1 3 5 7 

數字丟失並不重要,因爲它們僅用於排序,不記錄位置。

現在,讓我們假設您有4個項目按照索引排序。

1 2 3 4 

現在刪除第二項

1 3 4 

你有1個項目較少,但他們仍然在同一順序。假設你添加項目到最後:

1 3 4 5 

現在想象一下你想在年底移動項目是在第三的位置。這就是你問自己的另一個問題:爲什麼排序索引必須是整數?如果你使用浮點數,你可以看到很容易計算一個新的指數作爲之前的數字和之後的數字之間的中點。因此,在年底移動的項目是在第三的位置後,這是排序指標將是什麼樣子:

1.0 3.0 3.5 4.0 

如何在第二個位置添加一個數字。在這種情況下,中點很容易計算:

1.0 2.0 3.0 3.5 4.0 

所以。使用排序索引沒有任何問題。它不需要是一個整數,所以當你刪除一個項目時,你不必經過並重新計算所有的索引。

現在,這些浮點數很可能會變得輕率地增長,但是您可以在應用程序停機時或在用戶期望進行一些設置時發生更新期間定期重新編號。

+0

非常感謝@Abizern,向我解釋這個概念。 – Ranjit

+0

如果我已經理解了,你的回答,這是我應該做的,首先,我應該採用float類型的displayOrder屬性,當插入每個對象時應該增加該屬性。 – Ranjit

+0

是的,那大部分是我在說的 – Abizern