Reputation: 24675
I'm changing the text in a label within a UITableViewCell. I'm doing it in the main thread. I'm logging that I'm doing it to prove that my update is being called.
The update is not being reflected on-screen.
FWIW, I've done this dozens of times before and it has worked. I don't get what I might be doing differently this time.
I Googled for ideas. 99% of the answers are "make sure you dispatch to main thread" (I'm doing that), 3% are "make sure your tableview is wired up and your cellForIndexPath is being called" (that's done). The other 1.5% are miscellaneous horrid answers written by people guessing. ;)
Some code (the important bits are the last few lines. The rest is just setup):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// blah blah snip.
NSString * const cellID = NSStringFromClass([self class]);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellID];
cell.backgroundColor = [UIColor clearColor];
}
// blah blah more snip
// setup progress label
// >>>Solution<<< (thanks @Dennis !) -- wrong label here. Should be cellProgressTag
UILabel *lProgress = (UILabel*) [cell viewWithTag: cellDescriptionTag];
if (lProgress == nil)
{
float startX = 0;
CGRect lRect = lTitle.frame;
CGRect frame = CGRectMake(startX, (lRect.origin.y + lRect.size.height), (width - startX), 18);
lProgress = [[UILabel alloc] initWithFrame: frame];
lProgress.tag = cellProgressTag;
UIFont *font = lProgress.font;
lProgress.font = [font fontWithSize: 14];
lProgress.textAlignment = NSTextAlignmentCenter;
lProgress.textColor = textColor;
[cell.contentView addSubview: lProgress];
}
// blah blah more snip
// fill-out cell
NSInteger row = [indexPath row];
// blah blah more snip
lProgress.text = [NSString stringWithFormat: @"current level: %d %@", currentLevel, neededStr];
if ([key isEqualToString: @"EE"]) NSLog(@"%s %@ lProgress: %@", __PRETTY_FUNCTION__, (([NSThread isMainThread]) ? @"(main thread)" : @"NOT MAIN THREAD"), lProgress); // DEBUG
lDescr.text = [NSString stringWithFormat: @"%@%@", _descriptionPrefix, [oneTech objectForKey: @"description"]];
return cell;
}
Sample log output:
2015-04-11 09:11:01.074 worfc[20202:5918312] -[myapp tableView:cellForRowAtIndexPath:] (main thread) lProgress: <UILabel: 0x815bd5f0; frame = (30 53; 320 30); text = 'current level: 0 74.1...'; userInteractionEnabled = NO; tag = 102; layer = <_UILabelLayer: 0x815bd6b0>>
2015-04-11 09:11:03.925 worfc[20202:5918312] -[myapp tableView:cellForRowAtIndexPath:] (main thread) lProgress: <UILabel: 0x815bd5f0; frame = (30 53; 320 30); text = 'current level: 0 76.6...'; userInteractionEnabled = NO; tag = 102; layer = <_UILabelLayer: 0x815bd6b0>>
Yet the lProgress
label only ever updates if I hide & reshow the table (it's in a view controller I can pop up.) I'm trying to get the table to update live, as the result of a notification. (Yes, that's why I dispatch to main thread.)
Any hints?
Thanks!
Upvotes: 1
Views: 424
Reputation: 2165
Not 100% sure if that is the issue, but the tags of the label do not match. You try to get it with cellProgressTag
:
UILabel *lProgress = (UILabel*) [cell viewWithTag: cellDescriptionTag];
but when it is nil
, you create the label and set tag cellProgressTag
:
lProgress.tag = cellProgressTag;
That way, the next time you ask for the view with tag cellProgressTag
you will get nil
again.
Upvotes: 1