【发布时间】:2012-10-09 10:18:58
【问题描述】:
我正在为我的视图实现一个表视图控制器。作为苹果文档中的具体细节,我在表格中的单元格中使用了UITableViewCellStyleSubtitle。
我需要的是一个单元格,其中包含左侧的小头像、粗体 textLabel 和较小的 detailTextLabel。 textLabel 和 detailTextLabel 都是多行,而不仅仅是一行。
我正在尝试使用link 中的教程,但模拟器只显示textLabel。
这是我的cellForRowAtIndexPath: 方法:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [newsTitle objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.textLabel.numberOfLines = ceilf([[newsTitle objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20);
cell.detailTextLabel.text = [newsDescription objectAtIndex:indexPath.row];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
cell.detailTextLabel.numberOfLines = ceilf([[newsTitle objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20);
return cell;
}
这里是heightForRowAtIndexPath: 方法:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *titleString = [newsTitle objectAtIndex:indexPath.row];
NSString *detailString = [newsDescription objectAtIndex:indexPath.row];
CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height+titleSize.height;
}
注意:数组newsTitle保留textLabel、newsDescription的内容为textDetailLabel。它还没有包含头像。如果有人可以帮助我解决此问题并将较小的头像添加到此表格视图单元格中,我将不胜感激。
【问题讨论】:
-
你也必须在
cellForRowAtIndexPath方法中设置textLabel和detailTextLabel的高度.. -
你有没有得到解决方案?
标签: ios objective-c uiimageview uitableview