在我的应用程序中,我将按以下方式向每个表视图单元格添加3个UILabel。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ShowMoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [self getCellContentView:CellIdentifier];
return cell;
}
- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
cell.backgroundColor=[UIColor clearColor];
for(int j = 0; j < 3; j++)
[cell insertSubview:[items objectAtIndex:j] atIndex:j];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
这里的项目是NSMutableArray
,它有UILabel的元素。我想在每个labels
中添加三个cell
。但它只在最后一行显示3 labels
。
我在这里做错了吗?
由于
答案 0 :(得分:3)
您正尝试将相同的UILabel
对象添加到不同的单元格。 UIViews
(UITableViewCell
是其子类)只能有一个父视图。您应该为每个单元格创建3个新标签(如果它还没有包含它们)。
答案 1 :(得分:0)
视图对象(例如标签)不应以任何形式存储。解决此问题的最佳方法是将数据项存储在数组中,并使用单元格仅显示这些数据项。
创建自定义单元格
`
MyTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel * lbl1;
@property(nonatomic,strong)UILabel * lbl2;
@property(nonatomic,strong)UILabel * lbl3;
@end;
然后使用类似
将值设置为这些标签cell.lbl1.text = @" some text";
cell.lbl2.text = @" some text";
cell.lbl3.text = @" some text";
答案 2 :(得分:0)
UILabel *lblAdd=[[UILabel alloc]initWithFrame:CGRectMake(10, 60, 150, 20)];
lblAdd.backgroundColor =[UIColor clearColor];
lblAdd.textColor=[UIColor blackColor];
lblAdd.text=@"Label";
lblAdd.font=[UIFont fontWithName:@"Helvetica" size:12];
[cell.contentView addSubview:lblAdd];
就是你会在每一行都找到标签 快乐编码