滚动时UITableView重复单元格

时间:2011-08-14 11:38:26

标签: iphone objective-c uitableview

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

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

·H

@interface HomeViewController : UITableViewController {


    int numberOfRows;

    NSArray *allVaults;

}

@property (nonatomic, assign) int numberOfRows;
@property (nonatomic, retain) NSArray *allVaults;

@end

的.m

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    self.allVaults = [fileManager contentsOfDirectoryAtPath:vaultsPath error:nil];

numberOfRows = [self.allVaults count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberOfRows;
}

- (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];


        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];
    }
    return cell;
}

感谢任何帮助!

编辑1:图像显示当我将大多数代码移到(cell == nil)if语句之外时会发生什么:

之前:enter image description here

之后:enter image description here

编辑2:

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

2 个答案:

答案 0 :(得分:7)

当你从 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;
    }

更新更新的代码以解决第二个问题。重做AHCell以便类方法,例如, blankCell 会返回一个新单元格,其中包含设置的子视图和实例方法,例如 populateAHCellWithDictionary:设置内容。

答案 1 :(得分:2)

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

基本上在if语句中放入的每一行都不会改变,但是在if语句之外的行与行之间会有什么变化。

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

向下滚动屏幕顶部消失的单元格将被重复使用,并放在底部。这意味着你有100行,它不会创建100个单元格(它只创建一次可以在屏幕上显示的单元格数量并重复使用这些单元格),因为这会占用大量内存,并且滚动不会那么顺利。

相关问题