2012-12-11 52 views
0

我已使用init自定義類:添加第二個字幕行UITableViewCellStyleSubtitle

if(self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]) { 
} 

正如我想添加一個藍髮短信標籤的單元格的右側,並將它向右走選擇當否則看上去很奇怪時強調:

self.sizeTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width-110, 0.0, 100.0, 44.0)]; 
self.sizeTextLabel.textAlignment = NSTextAlignmentRight; 
self.sizeTextLabel.backgroundColor = [UIColor whiteColor]; 
self.sizeTextLabel.textColor = [UIColor colorWithRed:81/255.0 green:102/255.0 blue:145/255.0 alpha:1.0]; 
self.sizeTextLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight; 
[self.contentView addSubview:self.sizeTextLabel]; 

有可能做的更好的方式^

我想什麼做的就是添加另一個標籤,有效地爲另一個字幕行,然後使用heightForRowAtIndexpath方法創建單元格時,增加行的高度。

問題:當我向內容視圖添加一個新的標籤「行」時,它不會變得更高(默認視圖移到視圖中間)。如何在字幕視圖下正確創建和定位它?如果我要將第一個字幕改爲多行,那麼如果第二個標籤知道該怎麼做就會很好。

我希望可可有相對定位。或者我還沒有找到它!

回答

2

您可以詳細文本標籤多行和你的兩個字符串添加到一個標籤。在這個例子中,我的數據在它的字典三個按鍵,兩個字幕字符串「主」爲主要標籤的文本,「detail1和‘detail2’。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDictionary *object = _objects[indexPath.row]; 
    cell.textLabel.text = object[@"main"]; 
    NSString *concat = [NSString stringWithFormat:@"%@\n%@",object[@"detail1"],object[@"detail2"]]; 
    cell.detailTextLabel.numberOfLines = 2; 
    cell.detailTextLabel.text = concat; 
    return cell; 
} 
+0

我也認爲這卻不盡如人意時一個'副標題'太長,需要截斷或進入第二行,在兩個字幕之間需要稍微寬一些的間隔以明確它們是分開的。 – amcc

+0

@Vanthel,如果你想要的話,你總是可以在這兩行之間放置一個空格,並將numberOfLines設置爲0,允許不限數量的行。 – rdelmar