Irene
Irene

Reputation: 69

refresh cells in custom tableView

I have a tableView and a detailView where the user can change the data. It worked perfectly until I programmatically changed the cells from subtitle to custom. Now when I edit the data and I go back I see the new data overlapping the old one,this despite of the [self.tableView reloadData] in viewDidLoad.The data change is working well because if I relaunch the app the new data is displayed.

Also the separator lines between the cells are not covering the whole width.

Just to be sure here is the code for the custom cells:

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

UILabel *mainLabel, *detailLabel, *secondLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (!cell)
{
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 5.0, 220.0, 15.0)];
// mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = NSTextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |    UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];

secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 5.0, 200, 15.0)];
// secondLabel.tag = MAINLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:10.0];
secondLabel.textAlignment = NSTextAlignmentRight;
secondLabel.textColor = [UIColor whiteColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |    UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];

detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20.0, 220.0, 25.0)];
// detailLabel.tag = SECONDLABEL_TAG;
detailLabel.font = [UIFont systemFontOfSize:12.0];
detailLabel.textAlignment = NSTextAlignmentLeft;
detailLabel.textColor = [UIColor lightGrayColor];
detailLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |        UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:detailLabel];


SNMGps *aCell = [arrayOfCell objectAtIndex:indexPath.row];
mainLabel.text = [NSString stringWithFormat:@"%@", aCell.name];
secondLabel.text = [NSString stringWithFormat:@"%d",aCell.gpsID];
detailLabel.text =  [NSString stringWithFormat:@"%@",aCell.notes];
return cell;

}

Thanks.

Upvotes: 0

Views: 1859

Answers (4)

App Dev Guy
App Dev Guy

Reputation: 5546

Reload the whole section. I was having the same issues. This code worked perfectly for me, and it's only one line.

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

//don't forget to end refresh with 
[refreshControl endRefreshing];

As opposed to the multiple lines.

Why does it work? It reloads the entire section so however many cells you have in a section, this reloads them rather than trying to do a statement with a count or something fancy.

Upvotes: 1

Zaphod
Zaphod

Reputation: 7290

It's very probably because of the reuse of the cells. When you reuse a cell, it's not a new one (that's why it is so fast, you don't destroy and recreate a cell).

So the only thing you need, in the else of your if (!cell) is to remove all labels in your cell, and thing will be alright!

For instance:

if (!cell) {
    ...
}
else {
    for (int i = cell.contentView.subviews.count -1; i>=0; i--) {
        UIView * v = [cell.contentView.subviews objectAtIndex:i];
        if ([v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
}

Upvotes: 0

mashdup
mashdup

Reputation: 875

Have you tried just reloading the rows that are affected?

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

Upvotes: 0

Toseef Khilji
Toseef Khilji

Reputation: 17429

You have to reload tableview in viewWillAppear.

-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}

Upvotes: 0

Related Questions