Ios 在自定义单元格问题中添加UILabel
我在我的Ios 在自定义单元格问题中添加UILabel,ios,iphone,uitableview,custom-cell,Ios,Iphone,Uitableview,Custom Cell,我在我的UITableView中有一个自定义单元格,我想根据字符串的值在单元格中添加一个UILabel 这是我的手机代码 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary * tmpDictn = [tableAry objectAtIndex:indexPath.section];
UITableView
中有一个自定义单元格,我想根据字符串的值在单元格中添加一个UILabel
这是我的手机代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary * tmpDictn = [tableAry objectAtIndex:indexPath.section];
NSString * typeStr = [tmpDictn objectForKey:@“DocumentType”];
NSString * cellIdentifier = @"TestCell";
TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if ([typeStr isEqualToString:@“Text”]) {
UILabel * textLbl = [[UILabel alloc] init];
textLbl.backgroundColor=[UIColor clearColor];
textLbl.textColor=[UIColor lightGrayColor];
textLbl.userInteractionEnabled=NO;
textLbl.numberOfLines = 0;
textLbl.font = [UIFont fontWithName:@“Helvetica" size:16];
[textLbl setFrame:CGRectMake(30, 20, 250, 25)];
textLbl.text= [NSString stringWithFormat:@"%@ %i",[splitAry objectAtIndex:i],indexPath.section];
[cell addSubview:textLbl];
}
}
return cell;
}
MyUITableView
包含5个单元格(动态)。并且只有第一个单元格应该有这个标签(这也会根据文本而改变)。此代码在第一个单元格中添加UILabel
,但在第3和第5个单元格中也添加UILabel
我已经检查了它的相同的
UILabel
创建了1次,并添加到单元格1、3和5中。“文本”仅位于表格中的第一个位置。对于需要添加标签的单元格,尝试使用不同的单元格标识符,例如@“TextCell”。否则,您将重用已经添加了标签的单元格,即使它不应该存在。或者,您可以在if([typeStr isEqualToString:@“Text”])
块的“else”条件下删除标签(如果存在),但我认为前者更干净。您正在使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary * tmpDictn = [tableAry objectAtIndex:indexPath.section];
NSString * typeStr = [tmpDictn objectForKey:@“DocumentType”];
NSString * cellIdentifier = @"TestCell";
TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *lbl = [cell viewWithTag:1];
if(lbl)
{
[lbl removeFromSuperView];
}
if ([typeStr isEqualToString:@“Text”]) {
UILabel * textLbl = [[UILabel alloc] init];
textLabel.tag = 1;
textLbl.backgroundColor=[UIColor clearColor];
textLbl.textColor=[UIColor lightGrayColor];
textLbl.userInteractionEnabled=NO;
textLbl.numberOfLines = 0;
textLbl.font = [UIFont fontWithName:@“Helvetica" size:16];
[textLbl setFrame:CGRectMake(30, 20, 250, 25)];
textLbl.text= [NSString stringWithFormat:@"%@ %i",[splitAry objectAtIndex:i],indexPath.section];
[cell addSubview:textLbl];
}
}
return cell;
}
[tableView dequeueReusableCellWithIdentifier:cellIdentifier]
//Add your label here.
代码的问题是表视图正在重用单元格。因此,随着细胞数量的增加,它将出现在许多细胞中
我是一个有点老的程序员,但我认为最好的方法是
如果(!单元格)
{
cell=[[TestCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier]
//Add your label here.
}如果(!标签) {//你的代码 //如果不满足条件,则将标签文本设置为零 } 对您来说,一个快速的解决方案是放置一个else并将文本设置为nil
干杯。类型str值是常量还是动态您使用的是dequeueReusableCellWithIdentifier,因此一旦您将标签添加到单元格中,它将在下次重用时出现。您不会在任何地方删除标签,而只是添加。我建议将UITableViewCell子类化,创建自己的自定义单元格,并根据需要进行配置。如果要滚动表格视图,标签将更改其位置。原因是为了单元的可重用性,所有单元都使用相同的单元标识符。使用不同的单元格标识符来标识您希望与标签一起使用的单元格,其余单元格则使用不同的标识符。在另一个单元格中仍然没有添加相同的标签。Move
UILabel*lbl=[cell viewWithTag:1];if(lbl){[lbl removeFromSuperView];}
在if([typeStr isEqualToString:@“Text”])之前
有效。。但是如果我有多个标签和不同的TagU,但只有一个标签有Tag1呢?不,实际上我有多个标签,用于不同的单元格,有文档类型的文本。