gpt4 book ai didi

iphone - UITableView 滚动时重复单元格

转载 作者:行者123 更新时间:2023-12-03 20:22:14 26 4
gpt4 key购买 nike

当我向下滚动 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com