UITableView没有正确重新加载

时间:2011-04-15 14:07:25

标签: xcode uitableview ios4

解决

我的表格视图未正确刷新数据。问题是我把文本标签内容编辑部分放在“if(cell == nil)”条件中。

{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];

        //This is where the mistake was
        cell.textLabel.text = [[array objectAtIndex:indexPath.row] valueForKey:@"Key1"];

    if ([[[array objectAtIndex:indexPath.row] valueForKey:@"Key2"] intValue] == 1)
    { cell.detailTextLabel.text =
        [NSString stringWithFormat:@"%d sometext (%d sometext), %d sometext",
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key2"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key3"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key4"] intValue]]; }
    else { cell.detailTextLabel.text =
        [NSString stringWithFormat:@"%d sometext (%d sometext), %d sometext",
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key2"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key3"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key4"] intValue]]; }
    }

    return cell;
}

{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
    }

    //This is what it should have looked like in the first place
    cell.textLabel.text = [[array objectAtIndex:indexPath.row] valueForKey:@"Key1"];

    if ([[[array objectAtIndex:indexPath.row] valueForKey:@"Key2"] intValue] == 1)
    { cell.detailTextLabel.text =
        [NSString stringWithFormat:@"%d sometext (%d sometext), %d sometext",
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key2"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key3"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key4"] intValue]]; }
    else { cell.detailTextLabel.text =
        [NSString stringWithFormat:@"%d sometext (%d sometext), %d sometext",
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key2"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key3"] intValue],
         [[[array objectAtIndex:indexPath.row] valueForKey:@"Key4"] intValue]]; }
    return cell;
}

0 个答案:

没有答案