Некорректное обновление содержимого UITableView - PullRequest
0 голосов
/ 28 декабря 2011

У меня есть TableView, число строк которого зависит от количества строк NSStrings в NSMutableArray friendsNames.

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

Также каждая строка отображает эту строку NSString в соответствующем индексе friendsNames.Кажется, все очень просто.Но когда я удаляю строку из 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.it вызывается из 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 Ответ

2 голосов
/ 28 декабря 2011

Это проблема с клетками многократного использования.Просто измени свой код так:

- (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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...