Reputation:
How to clear whole tableview data and reload with new one?
Upvotes: 10
Views: 30692
Reputation: 9477
Swift: Not sure it's the best way or not, But I'm using this code to clear and roload Data:
fileprivate func clearTable() {
self.tableviewData.removeAll(keepingCapacity: false)
self.tableView.reloadData()
}
Upvotes: 0
Reputation: 522
By the way, clearsContextBeforeDrawing
is a property, and
[cell.contentView clearsContextBeforeDrawing];
does nothing except of returning its value.
Upvotes: 1
Reputation: 1
You can look at the value of the reused cell and if its not the same - then recreate it.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyLog(@"Start Table: cellForRowAtIndexPath: %d", indexPath.row);
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
UILabel *lblNew;
lblNew = [UILabel new];
UILabel *lblCell;
lblCell = [UILabel new];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
[cell.contentView addSubview:[lblNameArray objectAtIndex:indexPath.row]];
}
else
{
lblNew = [lblNameArray objectAtIndex:indexPath.row];
for (id label in [cell.contentView subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
lblCell = label;
}
}
if (![lblNew.text isEqualToString:lblCell.text])
{
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
[cell.contentView addSubview:[lblNameArray objectAtIndex:indexPath.row]];
}
}
return cell;
Upvotes: 0
Reputation: 4294
I had double entries on selection after scrolling. Now the Display is as clean as expected! Used the workaround
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
and also (btw: clearsContextBeforeDrawing as standalone it did not work
[cell.contentView clearsContextBeforeDrawing];
in Simulator i have the impression of more accurate reaction on cell selection. Thank you!
Upvotes: -1
Reputation: 345
ok.. but.. we need the solution for this.. if we add the cells in runtime, i have received the output with the same cells in first and last cells of the uitableview..
you can clear the cell before add any subviews
Example:
[mytableview.contentView clearsContextBeforeDrawing];
Upvotes: 0
Reputation: 31
This was my wrok around which worked. set cell = nil;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
Upvotes: 3
Reputation: 2512
Clear your data before calling reloadData, and now the user will see the table get cleared and re-populated with fresh data.
myArray = nil;
[myTableView reloadData];
Upvotes: 25