IOS自定义单元格,当单元格重用时显示错误文本的IOS自定义单元格
问题描述:
我一直在试图弄清楚这一点。我在它自己的xib文件中创建一个自定义单元格。在我的视图控制器中,我已经设置了一个表格视图控制器。正在被拖入表格视图的数据是基于一个来自我拥有的核心数据的获取请求控制器。我在cellForRowAtIndexPath函数中设置了自定义单元格。我为这个函数中的每个单元格创建一个标签,并用来自管理对象的一些数据填充标签。当我第一次运行时,一切似乎都好。但是,当我尝试上下滚动并重新使用新单元格时,标签中的数据将放置在错误的单元格中。我已经看到和听到这与细胞的再利用有关。但是,在纠正这个问题上还没有看到太多例子。下面是我在我的cellForRowAtIndexPath函数中的一些代码。让我知道是否可能需要其他输入。谢谢你的帮助。IOS自定义单元格,当单元格重用时显示错误文本的IOS自定义单元格
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
/* do this to get unique value per cell due to sections. */
NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1;
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *lastSession = nil;
UILabel *lastSessionLabel = nil;
if(cell == nil) {
lastSession = [managedObject valueForKey:@"last_session"];
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCell"];
self.tableView.backgroundColor = [UIColor clearColor];
cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)];
lastSessionLabel.textAlignment = UITextAlignmentLeft;
lastSessionLabel.tag = indexForCell;
lastSessionLabel.font = [UIFont systemFontOfSize:17];
lastSessionLabel.highlighted = NO;
lastSessionLabel.backgroundColor = [UIColor clearColor];
cell.contentView.tag = indexForCell;
[cell.contentView addSubview:lastSessionLabel];
} else {
lastSessionLabel = (UILabel *)[cell viewWithTag:indexForCell];
}
if (lastSession && lastSession.length) {
lastSessionLabel.text = lastSession;
}
cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@%@", @"Dr. ",
[managedObject valueForKey:@"first_name"],
@" " ,
[managedObject valueForKey:@"last_name"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.editingAccessoryType = UITableViewCellAccessoryNone;
return cell;
}
**修改过的代码**
下面是修改代码:在viewDidLoad中如下:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCell"];
self.tableView.backgroundColor = [UIColor clearColor];
}
in -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"CustomCell"];
NSInteger indexForCell = indexPath.section * 1000 + indexPath.row + 1;
NSLog(@"index for cell: %d",indexForCell);
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *lastSession = [managedObject valueForKey:@"last_session"];
UILabel *lastSessionLabel = nil;
if(cell == nil) {
NSLog(@"Cell is nil! %@", [managedObject valueForKey:@"first_name"]);
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
self.tableView.backgroundColor = [UIColor clearColor];
}
lastSessionLabel = [[UILabel alloc]initWithFrame:CGRectMake(410,55, 89, 35)];
lastSessionLabel.textAlignment = UITextAlignmentLeft;
lastSessionLabel.tag = indexForCell;
lastSessionLabel.font = [UIFont systemFontOfSize:17];
lastSessionLabel.highlighted = NO;
lastSessionLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lastSessionLabel];
/* Appropriate verbiage for nil last session. */
if (lastSession && lastSession.length) {
lastSessionLabel.text = lastSession;
}
return cell;
}
I am still having issues again with the label cell text changing when I scroll for different cells. I read some where about maybe having to use the prepareForReuse function for this.
答
你只取lastSession
当你创建一个新的细胞。请尝试在if(cell == nil)
声明之前放置此行。
lastSession = [managedObject valueForKey:@"last_session"];
I.e.这样的:
NSString *lastSession = [managedObject valueForKey:@"last_session"];
在此代替:
NSString *lastSession = nil;
UPDATE
您还设置了相同的标签为两种观点:基于您的代码
lastSessionLabel.tag = indexForCell;
...
cell.contentView.tag = indexForCell;
您只应使用第一行的样本,即为设置标签
第二次更新
你应该也只能在你看来生命周期调用registerNib:
一次,例如在viewDidLoad
,不是每次你需要一个新的单元格。此外,如果使用cell == nil
而不是使用dequeueReusableCellWithIdentifier:
,则应该创建一个新单元格。例如。
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
感谢您的及时回复。我试过想要你提到,但我仍然遇到随着标签文本随机变化的问题,当我上下滚动查看不同的单元格。 – doubleace3 2012-04-19 00:59:43
请参阅我的更新 – 2012-04-19 10:06:36
相同的问题。似乎滚动标签中的内容发生变化并且不一致。也许不同的方法是必要的。 – doubleace3 2012-04-19 16:04:54