删除uitableview中的行

时间:2011-11-04 07:31:03

标签: iphone objective-c ios xcode uitableview

我有一个应用程序,如果用户输入数据,行将使用该数据进行更新

我可以使用单个按钮说“删除”,单击该按钮会立即删除tableview中的所有行。?

3 个答案:

答案 0 :(得分:7)

是的,你可以这样做。首先从数据源中删除所有数据,然后重新加载表。对于前者 -

[yourArrayDataSource removeAllObjects];
[yourTable reloadData];

要动态删除行 - 请使用IBAction方法&将其链接到您的UIButton。只要按下按钮,您就会有一个流畅的动画效果,让您的所有行都淡出。

-(IBAction)deleteRows
{
    [yourTable beginUpdates];
    for(int i=0; i<[yourArrayDataSource count]; i++)
    {
        indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [self.searchResTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
    }
    [yourTable endUpdates];
}

您可以在此处使用各种动画 -

UITableViewRowAnimationBottom
UITableViewRowAnimationFade
UITableViewRowAnimationMiddle
UITableViewRowAnimationNone
UITableViewRowAnimationRight
UITableViewRowAnimationTop

答案 1 :(得分:3)

按一下按钮并按下按钮操作方法

-(IBAction)deleteRows
{
     [array removeAllObjects];
    [tableview reloadData];
}

答案 2 :(得分:3)

Srikar的回答让我走上正轨,但创造了许多额外的单项数组,并且调用deleteRowsAtIndexPaths远远超过了需要。

-(void)clearTable
{

    NSMutableArray *indexPaths = [NSMutableArray array];
    for(int i=0; i<[self.myArray count]; i++)
    {
        NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [indexPaths addObject:anIndexPath];
    }

    [self.myTableView beginUpdates];
    [self.myTableView deleteRowsAtIndexPaths:indexPaths  withRowAnimation:UITableViewRowAnimationFade];
    self.myArray = [NSArray array];
    [self.myTableView endUpdates];

}