2013-12-14 112 views
0

我一直在閱讀很多有關更新UITableView後,通過IndexPath和數據源,它似乎是[tableView startUpdates]刪除行,運行任何更新代碼,然後[tableView endUpdates]UITableView細胞不知名地刪除

儘管下面的許多例子,以一個T,並利用[tableView deleteRowsAtIndexPaths:<indexPaths> withRowAnimation:UITableViewRowAnimationTop]; ,我似乎無法讓我的行刪除以任何方式,但什麼如下描繪:

http://www.youtube.com/watch?v=l6ov1FR0R4g&feature=youtu.be(基本上,如果有n> 1行,該行將只是閃爍,如果有n = 1行,它將向外滑動,停止,然後消失)。

我真的很喜歡一個更流暢的行爲,就像行可以滑出來一樣。這裏是執行刪除的代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int idx = [indexPath indexAtPosition:1]; 
    LoLoanedItemDoc *itemDoc = [[self items] objectAtIndex:idx]; 

    // Delete notification 
    [[UIApplication sharedApplication] cancelLocalNotification:[[itemDoc data] notification]]; 

    [tableView beginUpdates]; 

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; 

    // Remove from datasource 
    [[self items] removeObject:itemDoc]; 

    [tableView endUpdates]; 

    // Delete folder 
    [itemDoc deleteDoc]; 
} 
+0

嘗試刪除開始和結束更新,在這種情況下不需要它們。 – rdelmar

+0

我總是先改變我的模型,然後做更新。當你告訴tableview刪除並且模型計數保持不變時,會有這樣的印象,即不這樣做會引發異常。 – danh

回答

1

我已經完成了兩種方式,這兩種方式都適用於我。

刪除項目,然後再執行更新。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     //Delete the item from the data source 
     [self removeProductFromQuoteAtIndexPath:indexPath]; 

     //Do the updates 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView endUpdates]; 
    } 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [shipping removeObjectAtIndex:indexPath.row - 1]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

你對小區進行任何類型的操作,它會刪除之前? (如關閉您擁有的抽屜動畫)。

+0

我仍然試圖解決您的解決方案,但似乎並沒有改變行爲。就細胞操作而言,不,我沒有這樣做。只需從數據源中簡單地刪除,從本地存儲中刪除並從tableview中刪除。 – dclowd9901

+0

您也可以嘗試將UITableViewRowAnimationTop更改爲UITableViewRowAnimationAutomatic。有時我的動畫有趣。 – random

+0

我打算給你「選定的答案」和後續結果:原來我在重置我的數據源並在動畫完成之前將我的視圖重新加載到其他地方。我剝離了這個方面(我不老實地知道我爲什麼要這樣做),它工作正常。 – dclowd9901