滚动时UITableView重复单元格 [英] UITableView repeating cells when scrolled

查看:79
本文介绍了滚动时UITableView重复单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i scroll down my UITableView, it starts showing me the same cells that i've already seen, and scrolling around a bit continues to put cells in the wrong place.

这是我正在使用的代码。如果需要任何额外的信息,请告知我们:

Here's the code i'm using. If anything additional is needed then let me know:

.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语句之外时会发生什么:

EDIT 1: Image to show what happens when i move most code outside the (cell == nil) if statement:

之前:

之后:

编辑2:

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


推荐答案

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

It looks as if you are only setting the cell content when the you're getting a nil back from dequeueReusableCellWithIdentifier. You need to set the cell contents every time, not just when you need to create a new cell.

- (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:设置内容。

Update updated code to address second issue. Rework AHCell so that the class method, e.g. blankCell returns a new cell with the subviews set up and the instance method, e.g. populateAHCellWithDictionary: sets the content.

这篇关于滚动时UITableView重复单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆