Первая строка в UITableview, отличается от других строк - PullRequest
3 голосов
/ 13 марта 2011

Мой UITableView должен иметь в первом ряду изображение. В чем проблема? что когда пользователь прокручивает таблицу вниз и, после этого, прокручивает вверх, поверх изображения появляются данные других строк! Знаете почему?

Это мой код (я использую UITableViewCell)

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
    return 73.0;
}
else {
    return 109.0;
}   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    if (indexPath.row == 0) {
        [[NSBundle mainBundle] loadNibNamed:@"customcell_3" owner:self options:NULL];
        cell = cellaNib_with_image;
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"customcell_2" owner:self options:NULL];
        cell = cellaNib;
    }


}

if (indexPath.row == 0) {
    UIImage *rowBackground;
    UIImage *selectionBackground;
    rowBackground = [UIImage imageNamed:@"image.png"];
    selectionBackground = [UIImage imageNamed:@"image.png"];
    cell.backgroundView = [[[UIImageView alloc] init] autorelease];
    ((UIImageView *)cell.backgroundView).image = rowBackground;
    cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

}
else {
        NSString *elemento = [NSString stringWithFormat:@"%@", [array objectAtIndex:indexPath.row]];
    UILabel *testoLabel = (UILabel*)[cell viewWithTag:1];
    testoLabel.text = elemento;
//ecc... here a take the other datas
}
return cell;
}

Спасибо!

1 Ответ

10 голосов
/ 13 марта 2011

Когда ячейки прокручиваются, они используются повторно.

Итак, первая ячейка может быть повторно использована для других ячеек, и наоборот.

Я бы использовал два CellIdentifiers, один для первой строки и второй для остальных строк.

Если indexPath.row == 0, создать / удалить из очереди ячейку с помощью CellID1, настроить ее и вернуть.

Если indexPath.row> 1, создать / удалить из очереди с помощью CellID2, настроить его и вернуть.

Если вы хотите продолжать использовать одиночный cellID, то перед их настройкой nil / сбросьте сначала все содержимое, чтобы удалить предыдущие данные.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...