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.
So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With