Iphone UITableView滚动时的重复单元格

Iphone UITableView滚动时的重复单元格,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,当我向下滚动我的UITableView时,它开始向我显示我已经看到的相同单元格,并且滚动一点继续将单元格放在错误的位置 这是我正在使用的代码。如果需要任何其他信息,请告诉我: h m 感谢您的帮助 编辑1:Image以显示将大多数代码移到(cell==nil)if语句之外时发生的情况: 之前: 之后: 编辑2: -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

当我向下滚动我的UITableView时,它开始向我显示我已经看到的相同单元格,并且滚动一点继续将单元格放在错误的位置

这是我正在使用的代码。如果需要任何其他信息,请告诉我:

h

m

感谢您的帮助

编辑1:Image以显示将大多数代码移到(cell==nil)if语句之外时发生的情况:

之前:

之后:

编辑2:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 82;
}

看起来,您只是在设置单元格内容时,才从dequeueReusableCellWithIdentifier获得一个nil。您需要每次设置单元格内容,而不仅仅是在需要创建新单元格时

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

    AHCell *cell = (AHCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        // create a new cell if there isn't one available to recycle
        // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell = [AHCell blankCell];

    }

    // set the contents of the cell (whether it's a new one OR a recycled one)
    NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"];

    NSString *dictionaryPath = [NSString stringWithFormat:@"%@/%@",
                                vaultsPath,
                                [self.allVaults objectAtIndex:indexPath.row]];

    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];

    cell.backgroundView = [AHCellCreation backgroundView];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.selectedBackgroundView = [AHCellCreation selectedBackgroundView];
    // cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell];
    [cell populateAHCellWithDictionary: dictionary];
    return cell;
    }

更新更新代码以解决第二个问题。返工一个单元格,使class方法,例如blankCell返回一个新单元格,其中设置了子视图,实例方法,例如populateAHCellWithDictionary:设置内容。

在这种情况下
AHCellCreation
类必须将子视图添加到单元格中,然后一次性设置文本?您需要在if语句中布局单元格(添加子视图、UILabels、UIImageView等,并设置它们的框架等)。并将内容设置在if语句之外

基本上,if语句中的每一行都不会改变,但每一行的改变都会放在if语句之外

这是因为if语句中的代码只有在创建单元格时才到达,几乎总是在创建表视图加载时在屏幕上可见的单元格


当您向下滚动时,屏幕顶部消失的单元格将被重新使用,并放在底部。这意味着您有100行,但它不会创建100个单元格(它只创建一次可以在屏幕上看到的单元格数量,并重用这些单元格),因为这将消耗大量内存,而且滚动也不会那么平滑。

AHCellCreation类中有什么?如果它设置单元格的文本等,那么它需要在
if(cell==nil)
语句之外。但是如果我把它放在那里,它会不会将单元格内容放在前一个单元格内容的顶部,每次我滚动时都会一次又一次?看我的答案,问题在于单元格的创建,您可能会更好地摆脱一个单元格创建,并将其直接放在cellForRowAtIndexPath中:这在一定程度上起作用-单元格开始相互重叠。我在上面贴了一张图片。你们的细胞都一样高吗?如果没有,您在tableView:heightForRowAtIndexPath:method中做什么?是的,都一样。在上面发布了这个方法。哦,看起来你只是在创建中不断添加新的子视图。看看你的代码。整个dequeueReusableCellWithIdentifier:方法试图最小化对象创建。因此,当您循环使用时,只需设置子视图的内容。再创造它们也没关系,而且往往速度足够快。但是,如果您只是将它们添加到现有单元格中(这是自您修复第一个问题以来发生的事情),您将看到重叠。重叠的组件意味着您正在执行类似于-addSubview的操作,而无需删除createCellWithDictionary中的旧组件。当单元未被回收时,重叠发生。每次添加新视图之前,都需要删除现有子视图。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 82;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

    AHCell *cell = (AHCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        // create a new cell if there isn't one available to recycle
        // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell = [AHCell blankCell];

    }

    // set the contents of the cell (whether it's a new one OR a recycled one)
    NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"];

    NSString *dictionaryPath = [NSString stringWithFormat:@"%@/%@",
                                vaultsPath,
                                [self.allVaults objectAtIndex:indexPath.row]];

    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];

    cell.backgroundView = [AHCellCreation backgroundView];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.selectedBackgroundView = [AHCellCreation selectedBackgroundView];
    // cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell];
    [cell populateAHCellWithDictionary: dictionary];
    return cell;
    }