2011-07-28 79 views
3

我有一個部分表格,其中包含部分,我想刪除特定的單元格交換並刪除此單元格,並從表格中刪除該單元格,並從數組中刪除。commitEditingStyle問題 - 刪除表格中的單元格

以及如何在刪除單元格時動畫。

對於我下面的代碼,但不工作請幫助做到這一點。

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [self.reports removeObjectAtIndex:indexPath.row]; 
     [tbl reloadData]; 
      [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade];  



    } 
} 

得多多個R & d再之後,我建立了新的代碼和它成功運行

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


if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 

    [[[self.reports objectAtIndex:indexPath.section] valueForKey:@"events"] removeObjectAtIndex:indexPath.row]; 
    [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationLeft]; 

[NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(tableReload) userInfo: nil repeats: NO]; 
} 

}

-(void)tableReload{ 
    [tbl reloadData]; } 
+0

你能分享你的'cellForRowAtIndexPath:'方法嗎? –

+0

我創建自定義單元格,並把它放到表 –

+0

我只是想看看你是如何分配文本到單元格,以檢查你是否刪除正確的數組對象。你提到了分段tableview,這意味着你需要檢索indexpath的行和段,但你只檢查行。無論如何,很好,你的問題現在得到解決 –

回答

9

首先你不應該在從表中刪除行之前Reload table。其次,就你的情況而言,你可以在該部分有多個部分和多個行。所以,你的

[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade]; 

將無法​​正常工作。
從數組中刪除對象將工作。您不必從表格中刪除行。只需重新加載表格:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [self.reports removeObjectAtIndex:indexPath.row]; 
     [tbl reloadData]; 
} 

但請確保您正確放置了正確的indexNumber。

+1

- (無效)的tableView:(UITableView的*)的tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 如果(editingStyle == UITableViewCellEditingStyleDelete){ \t \t [[[self.reports objectAtIndex: indexPath.section] valueForKey:@「events」] removeObjectAtIndex:indexPath.row]; \t \t [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationLeft]; } –

2

你是不是無終止您的NSArray的,這就是爲什麼該行不會從您的表視圖中刪除。

以下行:

[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] 
           withRowAnimation:UITableViewRowAnimationFade]; 

實際上應該是:

[tbl deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
           withRowAnimation:UITableViewRowAnimationFade]; 

通知IndexPath後爲零。 arrayWithObjects應該是一個零終止的對象列表

+0

經過某種類型的研發並在成功運行的代碼下開發。 –

+1

- (無效)的tableView:(UITableView的*)的tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 如果(editingStyle == UITableViewCellEditingStyleDelete){ \t \t [[[self.reports objectAtIndex:indexPath.section ] valueForKey:@「events」] removeObjectAtIndex:indexPath.row]; \t \t [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath。節]] withRowAnimation:UITableViewRowAnimationLeft]; } –