Почему мой UITableViewCell повторяется? - PullRequest
2 голосов
/ 30 ноября 2010

Когда я прокручиваю за пределами экрана, я получаю несколько повторяющихся ячеек. Как я могу это исправить?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = [NSString stringWithFormat:@"Bar Cafe",indexPath.row];
            break;
        case 1:
            cell.textLabel.text = [NSString stringWithFormat:@"Boutique Hester",indexPath.row];
            break;
        case 2:
            cell.textLabel.text = [NSString stringWithFormat:@"Boutique Hester Shoes",indexPath.row];
            break;
        case 3:
            cell.textLabel.text = [NSString stringWithFormat:@"Local Advice - Paulina",indexPath.row];
            break;
        case 4:
            cell.textLabel.text = [NSString stringWithFormat:@"Museum Brileyeglass",indexPath.row];
            break;
        case 5:
            cell.textLabel.text = [NSString stringWithFormat:@"Redlight Swans",indexPath.row];
            break;
        case 6:
            cell.textLabel.text = [NSString stringWithFormat:@"Reefer Flying Dutchman",indexPath.row];
            break;
        case 7:
            cell.textLabel.text = [NSString stringWithFormat:@"Reefer Tiani Hempshop",indexPath.row];
            break;

        default:
            break;
    }

    // Configure the cell...
    //cell.textLabel.text = [NSString stringWithFormat:@"Featured Event %d",indexPath.row];
    //cell.detailTextLabel.text = [NSString stringWithFormat:@"Description %d", indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Ответы [ 3 ]

0 голосов
/ 30 ноября 2010

Поскольку в методе cellForRowAtIndexPath ячейки используются повторно, и при каждой прокрутке индексный путь начинается с 0. Для более подробной информации вы можете использовать NSLog (@ "Index Path.row =% d", indexPath.row) см. в окне консоли.

0 голосов
/ 30 ноября 2010

Ваш код работает нормально для меня ... Нет prb на всех ..

Я думаю, вы хотите напечатать indexPath.row в каждой строке. Если это так, вы должны изменить его следующим образом.

[NSString stringWithFormat:@"Reefer Tiani Hempshop %d",indexPath.row];

Даже если numberOfRowsInSection: возвращает больше 8, шансов на репутацию не будет.

0 голосов
/ 30 ноября 2010
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

это правильная реализация метода numberOfRowsInSection, если вы возвращаете больше 8, тогда начинается проблема повторения строк.

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