Chandu
Chandu

Reputation: 695

Deleting rows in uitableview

I am Having an application where, if the user enters data the rows will be updated with that data

Can i use One Single Button say 'delete' which when clicked will delete all the rows in the tableview at once.?

Upvotes: 2

Views: 5349

Answers (3)

averydev
averydev

Reputation: 5727

Srikar's answer put me on the right track, but creates a lot of extra single item arrays, and calls deleteRowsAtIndexPaths far more than is needed.

-(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];

}

Upvotes: 3

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73688

Yes you can do that. First remove all data from your data source, then reload your table. For ex. -

[yourArrayDataSource removeAllObjects];
[yourTable reloadData];

To animate the deletion of rows - do this in an IBAction method & link it to your UIButton. As soon as you press the button you will have a smooth awesome animation making all your rows fade out.

-(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];
}

There are various animations that you can use here-

UITableViewRowAnimationBottom
UITableViewRowAnimationFade
UITableViewRowAnimationMiddle
UITableViewRowAnimationNone
UITableViewRowAnimationRight
UITableViewRowAnimationTop

Upvotes: 7

Tendulkar
Tendulkar

Reputation: 5540

make a button and in the button action method

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

Upvotes: 3

Related Questions