TableView 重用相同的单元格
在我的 UITableview 中,我收到一个奇怪的错误。我有一个名为 _cellTextArray 的字典数组,该字典包含键“textLabel”、“detailTextLabel”和“cornerLabel”。不过,我创建了三个 UILabel,它们被添加到单元格的 contentView 中,并且它们的文本被设置为字典中相应的名称。我的问题是 textLabels 仅检索字典中的前 5 个对象,并将它们用于每个单元格,而不是检索字典中单元格索引处的对象。
请你告诉我我做错了什么。我已经梳理了代码几次,NSLog'ing 每个索引路径的字典值,它们都是正确的。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CheckBox *btn = (CheckBox *)[_checkboxArray objectAtIndex:indexPath.row];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell.contentView addSubview:btn];
//I added this code:
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 3; // 0 means no max.
UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient7.png"]] autorelease];
[cell setBackgroundView:img];
UILabel *lab = [[[UILabel alloc] initWithFrame:CGRectMake(40, 18, cell.contentView.frame.size.width-15, 22)] autorelease];
[lab setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"textLabel"]];
[lab setBackgroundColor:[UIColor clearColor]];
[lab setTextColor:[UIColor whiteColor]];
[lab setAdjustsFontSizeToFitWidth:YES];
[lab setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lab];
UILabel *dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 54, cell.contentView.frame.size.width- 1, 22)] autorelease];
[dlabl setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"detailTextLabel"]];
[dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]];
[dlabl setBackgroundColor:[UIColor clearColor]];
// [dlabl setAdjustsFontSizeToFitWidth:YES];
[dlabl setTextAlignment:UITextAlignmentLeft];
[dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]];
[cell.contentView addSubview:dlabl];
UILabel *cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40, 19, 40, 20)] autorelease];
[cornerLabel setTextColor:[UIColor whiteColor]];
//[cornerLabel setFont:[UIFont systemFontOfSize:12]];
[cornerLabel setAdjustsFontSizeToFitWidth:YES];
[cornerLabel setBackgroundColor:[UIColor clearColor]];
[cornerLabel setTextAlignment:UITextAlignmentCenter];
[cell.contentView addSubview:cornerLabel];
[cornerLabel setAdjustsFontSizeToFitWidth:YES];
[cornerLabel setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"cornerLabel"]];
// Adds image to cell
// UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 140, 50)] autorelease];
// [imageView setImage:[UIImage imageNamed:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"image"]]];
// [cell.contentView addSubview:imageView];
}
// Configure the cell.
return cell;
}
In my UITableview, I am receiving a strange error. I have an array of dictionaries named _cellTextArray, and that dictionary contains the keys "textLabel", "detailTextLabel" and "cornerLabel". However I created three UILabel's which are added to the contentView of the cell and their text is set to their corresponding name in the dictionary. My problem is that the textLabels are only retrieving the first 5 objects in the dictionary, and using them for every cell rather than retrieving the object at the index of the cell in the dictionary.
Please could you tell me what I am doing wrong. I have combed through the code a few times, NSLog'ing the dictionaryValues for each indexPath, and they are all correct.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CheckBox *btn = (CheckBox *)[_checkboxArray objectAtIndex:indexPath.row];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell.contentView addSubview:btn];
//I added this code:
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 3; // 0 means no max.
UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient7.png"]] autorelease];
[cell setBackgroundView:img];
UILabel *lab = [[[UILabel alloc] initWithFrame:CGRectMake(40, 18, cell.contentView.frame.size.width-15, 22)] autorelease];
[lab setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"textLabel"]];
[lab setBackgroundColor:[UIColor clearColor]];
[lab setTextColor:[UIColor whiteColor]];
[lab setAdjustsFontSizeToFitWidth:YES];
[lab setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lab];
UILabel *dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 54, cell.contentView.frame.size.width- 1, 22)] autorelease];
[dlabl setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"detailTextLabel"]];
[dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]];
[dlabl setBackgroundColor:[UIColor clearColor]];
// [dlabl setAdjustsFontSizeToFitWidth:YES];
[dlabl setTextAlignment:UITextAlignmentLeft];
[dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]];
[cell.contentView addSubview:dlabl];
UILabel *cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40, 19, 40, 20)] autorelease];
[cornerLabel setTextColor:[UIColor whiteColor]];
//[cornerLabel setFont:[UIFont systemFontOfSize:12]];
[cornerLabel setAdjustsFontSizeToFitWidth:YES];
[cornerLabel setBackgroundColor:[UIColor clearColor]];
[cornerLabel setTextAlignment:UITextAlignmentCenter];
[cell.contentView addSubview:cornerLabel];
[cornerLabel setAdjustsFontSizeToFitWidth:YES];
[cornerLabel setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"cornerLabel"]];
// Adds image to cell
// UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 140, 50)] autorelease];
// [imageView setImage:[UIImage imageNamed:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:@"image"]]];
// [cell.contentView addSubview:imageView];
}
// Configure the cell.
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 此页面(iOS 表视图编程指南)了解如何正确完成。
Check Listing 5-3 on this page (iOS Table View Programming Guide) to see how it is done properly.
仅当没有可重复使用的单元格时才设置数据。一旦它们到达那里,表格视图就不会要求新的单元格。所以你的数据没有被设置。您显然是在返回重复使用的单元格。您应该在该单元格内设置所有静态属性(不依赖于行数据),并将所有赋值放在 if 语句之外。在本例中,它们似乎是三个标签的
setText:
语句。You are only setting the data when there are no reusable cells. Once they are there, the table view won't ask for a new cell. So your data isn't being set. You are plainly returning the reused cell. You should set all the static properties (which don't depend on the row data) within that cell and bring all assignments outside the if statement. In this case, they seem to be the
setText:
statements of the three labels.