如何在UITableViewCellStyleSubtitle的cell.detailText.label中添加两个标签?

Ash*_*osh 2 iphone objective-c uitableview

有没有办法我们可以为UITableViewCellStyleSubtitle的cell.detailText.label添加两个标签.我希望其中一个左对齐,另一个右对齐.

谢谢,

yin*_*kou 8

您无法向detailLabel添加标签,但可以将它们添加到单元格的contentView中.

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(20, 22, 140, 20)];
    UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(160, 22, 140, 20)];

    labelOne.text = @"Left";
    labelTwo.textAlignment = UITextAlignmentRight;
    labelTwo.text = @"Right";

    [cell.contentView addSubview:labelOne];
    [cell.contentView addSubview:labelTwo];
}


return cell;
Run Code Online (Sandbox Code Playgroud)

}