我有一张可滚动的表格。 在每个单元格中,我绘制了3个UILabel。前几个单元格正常。但是当我向下滚动桌子时,UILabels似乎正在吸引以前的UILabels。它就像单元格中的标签有一个已经清除的旧标签。我可以通过在每个重绘的整个单元格上绘制背景颜色来解决这个问题,但这仍然无法解释这个奇怪的问题及其发生的原因。
任何人都知道为什么会这样,解决方案可能是什么?
- (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];
}
Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row];
UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)];
teamName1Label.textAlignment = UITextAlignmentCenter;
teamName1Label.textColor = [UIColor redColor];
teamName1Label.backgroundColor = [UIColor clearColor];
teamName1Label.text = aMatch.teamName1;
[cell addSubview:teamName1Label];
UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)];
teamVersusLabel.textAlignment = UITextAlignmentCenter;
teamVersusLabel.textColor = [UIColor redColor];
teamVersusLabel.backgroundColor = [UIColor clearColor];
teamVersusLabel.text = @"V";
[cell addSubview:teamVersusLabel];
UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)];
teamName2Label.textAlignment = UITextAlignmentCenter;
teamName2Label.textColor = [UIColor redColor];
teamName2Label.backgroundColor = [UIColor clearColor];
teamName2Label.text = aMatch.teamName2;
[cell addSubview:teamName2Label];
return cell;
}
非常感谢 -code
答案 0 :(得分:4)
我认为这个问题的最佳解决方案是在中定义这些标签 - (UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)标识符withIndexPath:(NSIndexPath *)indexPath 方法
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)];
teamName1Label.textAlignment = UITextAlignmentCenter;
teamName1Label.textColor = [UIColor redColor];
teamName1Label.backgroundColor = [UIColor clearColor];
teamName1Label.text = aMatch.teamName1;
teamName1Label.tag = 1;
[cell.contentView addSubview:teamName1Label];
[teamName1Label release];
UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)];
teamVersusLabel.textAlignment = UITextAlignmentCenter;
teamVersusLabel.textColor = [UIColor redColor];
teamVersusLabel.backgroundColor = [UIColor clearColor];
teamVersusLabel.text = @"V";
teamVersusLabel.tag = 2;
[cell.contentView addSubview:teamVersusLabel];
[teamVersusLabel release];
UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)];
teamName2Label.textAlignment = UITextAlignmentCenter;
teamName2Label.textColor = [UIColor redColor];
teamName2Label.backgroundColor = [UIColor clearColor];
teamName2Label.text = aMatch.teamName2;
teamName2Label.tag = 3;
[cell.contentView addSubview:teamName2Label];
[teamName2Label release];
return cell;
}
现在cellForRowAtIndexPath方法将是 -
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=nil;
static NSString *Identifier = @"Cell";
cell = [theTableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil){
cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath];
}
Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
label.text = aMatch.teamName1;
label = (UILabel *)[cell.contentView viewWithTag:2];
label.text = @"V";
label = (UILabel *)[cell.contentView viewWithTag:3];
label.text = aMatch.teamName2;
return cell;
}
试试这段代码。希望它有帮助:)
答案 1 :(得分:0)
这是一个细胞重用问题。每次调用cellForRowAtIndexPath时,代码都会向单元格添加标签(即使单元格不是nil)。
滚动时,会加载已有标签的上一个单元格,然后代码会在旧标签上添加新标签。
仅为if (cell == nil)
部分中的单元格创建并添加标签。创建标签时,请在标签上设置标签。
然后在if
之外,使用viewWithTag
使用标签检索标签并设置文字。