开发者

iPhone: UITableView not Displaying New Data After Call to reloadData

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 corre开发者_StackOverflow社区ct. 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];
}


try

cell.textLabel.text = $VALUE;

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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜