为什么我的UITableViewCell重复?

问题描述 投票:2回答:3

当我在屏幕外滚动时,会得到一些重复的单元格。我该如何解决?

// 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;
}
iphone cocoa-touch uitableview
3个回答
0
投票
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

这是numberOfRowsInSection方法的正确实现,如果返回的结果大于8,则会出现重复行的问题。


-1
投票

由于在cellForRowAtIndexPath方法中,单元被重用,并且每当u滚动时,索引路径将从0开始。有关更多详细信息,您可以使用NSLog(@“ Index Path.row =%d”,indexPath.row)并查看在控制台窗口中。


-1
投票

您的代码对我来说很好,没有问题。

我认为您想在每一行上打印indexPath.row。如果是这样,则必须进行如下更改:

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

即使numberOfRowsInSection:返回超过8,也没有获得声誉的机会。

© www.soinside.com 2019 - 2024. All rights reserved.