And*_*rew 2 iphone objective-c uitableview
当我向下滚动我的UITableView时,它开始向我显示我已经看过的相同单元格,并且滚动一点继续将单元格放在错误的位置.
这是我正在使用的代码.如果需要任何其他内容,请告诉我:
.H
@interface HomeViewController : UITableViewController {
int numberOfRows;
NSArray *allVaults;
}
@property (nonatomic, assign) int numberOfRows;
@property (nonatomic, retain) NSArray *allVaults;
@end
Run Code Online (Sandbox Code Playgroud)
.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;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
编辑1:图像显示当我将大多数代码移到(cell == nil)if语句之外时会发生什么:
之前: 
后: 
编辑2:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 82;
}
Run Code Online (Sandbox Code Playgroud)
当您从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;
}
Run Code Online (Sandbox Code Playgroud)
更新更新的代码以解决第二个问题.重做AHCell,以便类方法,例如blankCell返回一个新的单元格,其中设置了子视图和实例方法,例如populateAHCellWithDictionary:设置内容.
| 归档时间: |
|
| 查看次数: |
5241 次 |
| 最近记录: |