UITableView custom cell not updated

There are many questions about updating user table cells, but this question is odd. I have a tableView with each cell, which now has custom UILabel and UIImageView .

Currently, there are only 2 cells. The first shows the date. When the table is first loaded, it displays the current date as a row in the first UILabel cells.

When I select the first cell, I present a custom class that handles all date pickers. After the date has been selected, this view opens, and I return to the table view.

In -(void)viewDidAppear table data is scanned and the new selected date will be displayed.

However, the label in the first cell is not updated.

What confuses the question is that if I have several cells displaying the same data, they will all be updated and display a new date, as expected. It seems that the cell row in index: 0 will not be updated.

What confuses the question even more: when I request the string UILabel value for a cell, it returns the correct date.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; // // clears the grouped style border from each table cell // UIView *clearBgView = [[UIView alloc]initWithFrame:CGRectZero]; [cell setBackgroundView:clearBgView]; // label UILabel *dl = [[UILabel alloc]initWithFrame:CGRectMake(70.0f, 10.0f, screenWidth-100, 50)]; [self setDetailsLabel:dl]; dl = nil; [[self detailsLabel] setBackgroundColor:[UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.1 ]]; [[self detailsLabel] setTextColor:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:.3f]]; //icon for each cell UIImageView *ci = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 50.0f, 50.0f)]; [ci setBackgroundColor:[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:.2]]; [self setCellIcon:ci]; ci = nil; // // set up views // [cell addSubview:[self cellIcon]]; [cell addSubview:[self detailsLabel]]; } // Configure the cell... [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; //populate each by row. NSString *dateDisplay = [self formatDate:[self dateCaught]]; NSLog (@"date is %@", dateDisplay); [[self detailsLabel] setText:[self formatDate:[self dateCaught]]]; switch (indexPath.row) { //this needs to be an integer, so return the row of the indexPath. case 0: NSLog (@"text for the cell is %@",[[self detailsLabel]text]); break; default: break; } return cell; 

}

+4
source share
2 answers

The problem is how you go through detailsLabel . You keep it owned or ivar on self

 [self setDetailsLabel:dl]; 

but you only install it when the cell is not available for reuse. When you reuse a cell, detailsLabel on self set to the label from the previous run, causing all kinds of problems.

The cleanest solution is to create your own class derived from UITableViewCell , move the initialization code, which creates labels, icons, background colors, etc. to the designated initializer, and create properties for setting label texts. Using this class, you can simplify the code as follows:

 UIMyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UIMyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [[cell detailsLabel] setText:[self formatDate:[self dateCaught]]]; // ^--- detailsLabel can be moved to UIMyCustomTableViewCell now 
+4
source

The solution is to not use a member variable like @property detailsLabel , but instead use tags for all user subzones. Change your code as follows:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { [...] // label UILabel *dl = [[UILabel alloc]initWithFrame:...]; dl.tag = 99; [cell.contentView addSubview: dl]; [...] } [...] UILabel *dl = [cell.contentView viewWithTag: 99]; [dl setText:[self formatDate:[self dateCaught]]]; [...] return cell; } 
0
source

All Articles