How to add two labels in cell.detailText.label of UITableViewCellStyleSubtitle?
Is there a way we can add two labels to cell.detailText.label of UITableViewCellStyleSubtitle. i want one of them left aligned and other one as right aligned.
Than开发者_开发技巧ks,
You can't add labels to the detailLabel, but you can add them to the contentView of the cell.
- (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;
}
This is not possible without cell subview customisation, as yinkou shows ... unless you concatenate the two strings together, and show both text strings on one line:
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", firstString, secondString];
Not quite sure if I understand you corretly but maybe you are looking for this:
UITableViewCellStyleValue1 A style for a cell with a label on the left side of the cell with left-aligned and black text; on the right side is a label that has smaller blue text and is right-aligned. The Settings application uses cells in this style. Available in iOS 3.0 and later. Declared in UITableViewCell.h.
A pretty old question, but I figured I'd chime in anyway.
If you are not using the accessory view of your cell, you can create a UILabel and assign it to the accessoryView
property. I have done this several times and works well.
精彩评论