IPhone: UITableView does not display new data after call to reload

My problem is that cell.textLabel does not display new data after reload. I can see it being cellForRowAtIndexPath

called so that I know the call reloadData

is going through. If I log into the log rowString

, I see the correct value, so the line I set the labels text to is correct. What am I doing wrong?

I have the 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];
}

      

+2


a source to share


3 answers


try

cell.textLabel.text = $VALUE;



If that doesn't help, are you sure you have set tableView.delegate AND tableView.dataSource?

0


a source


Try:

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

      



You now have an unusual design and may be preventing the user interface from updating. In the methods that customize the view, you want to call the superclass method before the subclass's operations. You are rearranging the order in the methods that break the view. Usually you don't need to call viewWillAppear super unless you have a custom superclass.

0


a source


I bet your cell.textLabel

reset somehow is zero. In my experience, I find it easiest to treat a method cellForRowAtIndexPath:

as if it always creates a new cell. Even when I use the cell again, I want to be ready for anything.

The header file for cell.textLabel

specifies that the default is nil. This means you want to assign the label to textLabel before navigating to its text property.

To do this, replace:

[[cell textLabel] setText:rowString];

      

from:

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];

      

0


a source







All Articles