在UITableView中放置标签时遇到问题 [英] Trouble with putting label in UITableView
问题描述
我使用此代码显示2段的 UITableView
I use this code to display UITableView
with 2 segment
if(segment.selectedSegmentIndex==0)
{
firstSeg=[firsArray objectAtIndex:indexPath.row];
NSString * celldata = [NSString stringWithFormat:@"%@ %@ %@",firstSeg.a,firstSeg.b,firstSeg.c];
lbl =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 20) ];
lbl.text=celldata;
[cell.contentView addSubview:lbl];
}
else if(segment.selectedSegmentIndex==1) {
seconSeg=[secondArray objectAtIndex:indexPath.row];
NSString * celldata = [NSString stringWithFormat:@"%@ %@ %@",secondSeg.a,secondSeg.b,secondSeg.c];
lbl2 =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 20) ];
lbl2.text=celldata;
[cell.contentView addSubview:lbl2];
}
return cell;
我的 tableView
和我的段工作正常,但是当我在第二段中选择一行时,第一个标签 lbl
和 lbl2
出现一起.我尝试在第二部分中使 lbl = nil
, lbl = NULL
无效,但是请您帮我吗?
My tableView
and my segment work fine , but when I select a row in the second segment, the first label lbl
and lbl2
appear together. I tried, in the second segment, to make lbl=nil
, lbl=NULL
but it don't work, can you help me please?
与此cell.textLabel.text = celldata;效果很好,但是文本太大,我不能修改大小吗?
with this cell.textLabel.text =celldata; it woek perfectly but the text is too large, i can't modifie the size ?
推荐答案
问题在于,每次重新加载tableview单元格时,您都将标签添加到单元格中
The problem is that you are adding the label to the cell every time you reload the tableview cells
尝试仅添加标签一次,并为此标签指定标签
Try adding the label only once and specify a tag for this label
在文件顶部添加:
#define CELL_LABEL_TAG 333
在创建单元格时,还要创建标签并将其添加为子视图
when you create the cell, create also the label and add it as a subview
UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 20) ];
[lbl setTag:CELL_LABEL_TAG];
[cell.contentView addSubview:lbl];
将您的代码更改为:
if(segment.selectedSegmentIndex==0) {
firstSeg=[firsArray objectAtIndex:indexPath.row];
NSString * celldata = [NSString stringWithFormat:@"%@ %@ %@",firstSeg.a,firstSeg.b,firstSeg.c];
lbl =(UILabel *)[cell.contentView viewWithTag:CELL_LABEL_TAG];
lbl.text=celldata;
}
else if(segment.selectedSegmentIndex==1) {
seconSeg=[secondArray objectAtIndex:indexPath.row];
NSString * celldata = [NSString stringWithFormat:@"%@ %@ %@",secondSeg.a,secondSeg.b,secondSeg.c];
lbl2 =(UILabel *)[cell.contentView viewWithTag:CELL_LABEL_TAG];
lbl2.text=celldata;
}
return cell;
这篇关于在UITableView中放置标签时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!