views:

244

answers:

3

My problem is that the cell.textLabel does not display the new data following a reload. I can see the cellForRowAtIndexPath being called so I know the reloadData call goes thru. If I log the rowString I see the correct value so the string I set label text to is correct. What am I doing wrong?

I have following code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    static NSString *RowListCellIdentifier = @"RowListCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RowListCellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RowListCellIdentifier]autorelease];
    }

    NSMutableString *rowString = [[NSMutableString alloc] init];
    [rowString appendString:[[[rows objectAtIndex:row] firstNumber]stringValue]];
    [rowString appendString:@" : "];
    [rowString appendString:[[[rows objectAtIndex:row] secondNumber]stringValue]];
    [rowString appendString:@" : "];
    [[cell textLabel] setText:rowString];

    [rowString release];
    return cell;
}

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

try

cell.textLabel.text = $VALUE;

if it doesnt help, are you sure that you have set the tableView.delegate AND the tableView.dataSource?

antihero
That shouldn't matter. He just using the old style reference.
TechZen
Yes i am sure because the table shows the data so the connection must be ok right?
donnib
actually it was the delegates and the datasource which was messed up, thx
donnib
A: 

Try:

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

What you have now is an unusual construction and might be preventing updates to the UI. In methods that set up a view, you want to call the superclass method before the subclass operations. You reverse the order in methods that tears down a view. You usually don't have to call the viewWillAppear of the super unless you have a custom superclass.

TechZen
Thank you for your suggestion. I just tried it but it did not make any difference on the update problem.
donnib
Wait a minute. Is viewWillAppear the only place you call reloadData? If so, then your table will only update after the table is removed from view and then displayed again.
TechZen
Yes and that's exactly what i do. I go into the detail view, change data then pop view which causes the viewWillAppear to fire and reloadData and i am logging the data to console and i know data is ok but still not visible in the UI
donnib
You only need to call reload data when the data changes while the table is displayed. The table will automatically load itself when it first appears so the reload data you have now is redundant.
TechZen
Try logging cell.textLabel.text right after you set it to see what value it has. That might help you narrow things down. Where (what line) did you log rowString?
TechZen
I have been doing a NSLog of rowString just before [rowString release] and data looks correct.
donnib
How about the label text?
TechZen
I'll try that out and get back with the results
donnib
I added following just before return cell and data looks fine:NSLog(@"%@",[cell.textLabel text]);NSLog(@"%@", rowString);this is very weird, now spend 2 days on this
donnib
I'm stumped. The code you have posted is correct. If the cell.textLabel.text is showing as correct and the `cellForRowAtIndexPath:` is being called when it should be, then you've got a very strange problem. The only thing I can think is that somehow, you not populating the correct instance of UITableView. You might try logging the address of the table sent to `cellForRowAtIndexPath:` and make sure it is the correct table. When all else fails log everything including the indexPath at the start and the end of the method.
TechZen
A: 

I bet your cell.textLabel is somehow being reset to nil. In my experience I find it easiest to treat the cellForRowAtIndexPath: method as if it's always creating a new cell. Even when it's reusing a cell I want to be ready for everything.

The Header file for cell.textLabel state that the default value is nil. This means that you want to assign a label to the textLabel before you go about changing it's text property.

To do that, replace:

[[cell textLabel] setText:rowString];

with:

UILabel *label = [[UILabel alloc] init];//or initWithFrame:
label.text = rowString;
/* Insert your own customization here */
label.font = [UIFont boldSystemFontOfSize:13.0];
label.backgroundColor = [UIColor clearColor];
label.adjustsFontSizeToFitWidth = YES;
cell.textLabel = label;
[label release];
fbartho