0

在我的应用程序中,我通过以下方式将 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;
}

这里的 items 是一个NSMutableArray,它有 UILabel 的元素。我试图labels在每个中添加三个cell。但它labels仅在最后一行显示 3。

我在这里做错了吗?

谢谢

4

3 回答 3

3

您正在尝试将相同的UILabel对象添加到不同的单元格。UIViews(它UITableViewCell是一个子类)只能有一个父视图。您应该为每个单元格创建 3 个新标签(如果尚未包含它们)。

于 2013-05-21T09:24:53.217 回答
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];

就是这样,您会在每一行中找到标签 Happy Coding

于 2013-05-21T10:29:25.417 回答
0

视图对象(例如标签)不应该以任何形式存储。解决此问题的最佳方法是将数据项存储在数组中并使用单元格仅显示这些项。

创建一个自定义单元格说

`

MyTableViewCell : UITableViewCell

@property(nonatomic,strong)UILabel * lbl1;

@property(nonatomic,strong)UILabel * lbl2;

@property(nonatomic,strong)UILabel * lbl3;

@end;

然后使用 like 将值设置为这些标签

cell.lbl1.text = @" some text";
cell.lbl2.text = @" some text";
cell.lbl3.text = @" some text";
于 2013-05-21T09:43:30.647 回答