I need some help for my problem. I’ve seen a lot of similar posts, and tried solutions, but none of them have worked for me.
I have a custom UITableViewCell in which I have multiple UILabel and a UIButton control. This control is suppose to update one of the labels from its cell. However, instead of updating the correct UILabel it updates the UILabel in the last cell.
My code :
- (UITableViewCell *)tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSInteger ETag = 111;
static NSInteger STag = 112;
.... // code
if (cell == nil) {
.... // code
frame.origin.x = 85;
frame.origin.y = 10;
frame.size.height = 50;
frame.size.width = 100;
SLabel = [[UILabel alloc] initWithFrame:frame];
SLabel.tag = STag;
[cell.backgroundView addSubview:SLabel];
//[SLabel release];
frame.origin.x = 215;
frame.origin.y = 10;
frame.size.height = 50;
frame.size.width = 100;
ELabel = [[UILabel alloc] initWithFrame:frame];
ELabel.tag = ETag;
[cell.backgroundView addSubview:ELabel];
//[ELabel release];
.... // code
}
.... // code
ELabel = (UILabel *) [cell.backgroundView viewWithTag:ETag];
SLabel = (UILabel *) [cell.backgroundView viewWithTag:STag];
ELabel.text = [s._episode stringValue];
ELabel.backgroundColor = [UIColor clearColor];
ELabel.textColor = [UIColor whiteColor];
ELabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
ELabel.textAlignment = UITextAlignmentLeft;
ELabel.tag = 1000+indexPath.row;
SLabel.text = [s._season stringValue];
SLabel.backgroundColor = [UIColor clearColor];
SLabel.textColor = [UIColor whiteColor];
SLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
SLabel.textAlignment = UITextAlignmentLeft;
SLabel.tag = 1000+indexPath.row;
..... // code
return cell;
}
Thanks for reading and helping me.
Tommy
Thank you ! i resolved my problem with :
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UILabel *a = (UILabel *) [cell viewWithTag:(1000+indexPath.row)];
a.text = [NSString stringWithFormat:@"%@", @"something"];
the issue was that tags were not unique
You access needful
UILabelvia itstag, if it is unique in your view.For example, you have
UITableViewwith multiple cells. Each cell containsUILabelthat might be updated. Let’s set unique tags for that labels, let them be yourindexPath.rowofUITableViewCell(it is already done in your code).Then when you want to update some cell with
tag == cellsTagToUpdateyou should just get reference to that cell via call toUITableView *tableView:UILabel *labelToUpdate = [tableView viewWithTag:cellsTagToUpdate].Now you have reference to your label that you want to update. Before updating you should check if
cellis not nil. It would be nil if that view (UILabel) is not now visible and was removed fromsuperview.