Martin Pilch
Martin Pilch

Reputation: 3295

UITableView delete all rows at once

How is it possible to delete all rows from UITableView at once? Because when I reload table view with new data, I am still inserting new rows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    }

    //... setting the new cell here ...

    return cell;
}

Thank you.

Upvotes: 19

Views: 47302

Answers (5)

Chad Hine
Chad Hine

Reputation: 211

FYI, The solution above "Delete all of your dataSource, then call reloadData." sounds like they are advocating this:

TableView.dataSource = nil;
[TableView reloadData];

I tried that and it seemed to work. Except, in iOS 8.1, I got random crashes. And I do mean random, as in, I could execute that code 10x and it would crash 1x. So, I used the recommended answer of:

[myArray removeAllObjects];
[tableView reloadData];

and now I am good.

Upvotes: 8

safaa elgendi
safaa elgendi

Reputation: 239

first delete your old data

myArray = nil;
[tableView reloadData];

then load your new data

myArray = newData;
[tableView reloadData];

Upvotes: 0

WrightsCS
WrightsCS

Reputation: 50707

Delete all of your dataSource, then call reloadData.

Upvotes: 4

dredful
dredful

Reputation: 4388

It sounds like you are not clearing your collection of data (NSArray) before your add more data. The UITableView will usually show all of your array data. If you don't want previous rows/data then you should empty your array prior to adding new information to it. Then call [yourTableView reload]

Upvotes: 2

Legolas
Legolas

Reputation: 12325

Do this.

Before you are reloading the table with

[tableView reloadData];

Remove all objects from your tableView array (The array which populated your tableView) For example.

[myArray removeAllObjects];
[tableView reloadData];

Upvotes: 49

Related Questions