2011-07-28 82 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]; } –