UITableView的更新内容不正确

时间:2011-12-28 17:10:00

标签: iphone uitableview

我有TableView,其行数取决于NSMutableArray friendsNames中NSStrings的数量。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return friendsNames.count + 1;
}

此外,每行在friendsNames的相应索引处显示NSString。一切似乎都很简单。但是当我从friendsNames中删除一个字符串并使用reloadData方法时,会发生奇怪的事情:UITableView删除了LAST行,而不是带有刚刚从friendsNames中删除的字符串的行。你能解释一下我发生了什么,我该怎么做才能解决它?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];

MyTableCell *cell = (MyTableCell *)[friendsList dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

    //create columns
    for (int i = 0;i < 6;i++)
        [cell.contentView addSubview:[self createGrid:i :indexPath]];
}
return cell;
}

这里是创建columns的方法。从cellForRowAtIndexPath调用它,它非常难看

- (UILabel *)createGrid:(int)columnIndex :(NSIndexPath *)indexPath
  {
CGFloat widths    [6] = {35.0,62.0,35.0,35.0,35.0,35.0};//two arrays holding widths of the columns and points where left sides begin
CGFloat leftSides [6] = {0.0,35.0,97.0,132.0,167.0,202.0};

NSArray *titles = [[[NSArray alloc] initWithObjects:@"Status",@"ID",@"Wins",@"Losses",@"Withdrawls",@"Win %", nil] autorelease]; 

UILabel *columnLabel = [[[UILabel alloc] initWithFrame:CGRectMake(leftSides[columnIndex],0.0,widths[columnIndex], friendsList.rowHeight)] autorelease];

if (indexPath.row == 0)
    columnLabel.text = [titles objectAtIndex:columnIndex];

else
{
    switch (columnIndex)
    {
        case 0:
        {
            BOOL isOnline = [[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:0] boolValue];
            columnLabel.text = isOnline ?@"On" :@"Off"; 
        }   
            break;
        case 1:
            columnLabel.text = [friendsNames objectAtIndex:indexPath.row - 1];
            break;
        case 2:
            columnLabel.text = [NSString stringWithFormat:@"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:1] intValue] ];
            break;
        case 3:
            columnLabel.text = [NSString stringWithFormat:@"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:2] intValue] ];
            break;
        case 4:
            columnLabel.text = [NSString stringWithFormat:@"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:3] intValue] ];
            break;
        case 5:
            columnLabel.text = [NSString stringWithFormat:@"%f",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:4] floatValue] ];
            break;
    }
}

columnLabel.layer.borderColor = [[UIColor blackColor] CGColor];
columnLabel.layer.borderWidth = 1.0;
columnLabel.font              = [UIFont systemFontOfSize:8.0];
columnLabel.textAlignment     = UITextAlignmentCenter;
columnLabel.textColor         = [UIColor blackColor];
columnLabel.autoresizingMask  = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;

return columnLabel;
 }

1 个答案:

答案 0 :(得分:2)

这是一个可重复使用的细胞问题。只需更改您的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *myIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];

    MyTableCell *cell = (MyTableCell *)[friendsList dequeueReusableCellWithIdentifier:myIdentifier];

    if (cell == nil) {
        //Create a new cell
        cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:myIdentifier] autorelease];
    }

    //Configure the cell
    //Remove all columns
    for(UIVIew *subview in cell.contentView.subviews) {
        [subview removeFromSuperview];
    }
    //Create columns
    for (int i = 0;i < 6;i++) {
        [cell.contentView addSubview:[self createGrid:i :indexPath]];
    }
    return cell;
}
相关问题