iphone - UITableView 滚动时重复单元格

标签 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;
}

最佳答案

看起来好像您只是在从dequeueReusableCellWithIdentifier返回零时才设置单元格内容。您每次都需要设置单元格内容,而不仅仅是在需要创建新单元格时。

- (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 返回一个新单元格,其中设置了 subview 和实例方法,例如populateAHCellWithDictionary:设置内容。

关于iphone - UITableView 滚动时重复单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7056578/

相关文章:

iphone - 如何根据给定的坐标移动 UIImage

iphone - 我需要在 Core-plot 的散点图中显示 x 和 y 轴的主要和次要线和点

objective-c - 什么对象应该实现 NSCollectionViewDelegate?

ios - 从表格 View 之外的另一个 View 中检索自定义单元格中的文本字段数据

iphone - 如何在 iPhone 的 monotouch 中创建 SQLite 数据库?

iphone - 在自动旋转时重置 UIScrollView 的 zoomScale 属性

ios - 当表格处于编辑模式时,在 UITableView 部分标题上切换 UIButton

ios - Game Center View 关闭后的启动方法

ios - 如何自动滚动 UITableView 单元格

swift - 将事件绑定(bind)到 Observable<JSON> 的键