2012-10-09 51 views
1

我为我的视图实现了表视图控制器。作为苹果文档中的具体细节,我在表中使用了UITableViewCellStyleSubtitle作为我的单元格。UItable查看单元格textLabel,detailTextLabel和UIImage

我需要的是一个单元格包含左侧的小化身,大胆的textLabel和更小的detailTextLabeltextLabeldetailTextLabel都是多行,不只一行。

我正在尝试从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内容,newsDescriptiontextDetailLabel。它尚未包含头像。我非常感谢,如果有人可以帮助我解决这个问题,并将小型化身添加到这个表格视图单元格。

+0

也u必须设置为textLabel和detailTextLabel的高度'cellForRowAtIndexPath'方法.. – vishy

+0

做你的解决方案或不? – Pratik

回答

0

你应该创建一个自定义UITableViewCell子类,有一个UIImageView您可以与您所选择的cellForRowAtIndexPath:UIImage填充属性。我建议添加UIImageView作为contentView的子视图,然后将单元格的缩进调整为UIImageView的宽度以及一些填充。这具有细胞的全部内容移动到右侧,以腾出空间为您的图像的效果:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 

    if (self) { 
     // make room for the icon 
     self.indentationLevel = 1; 
     self.indentationWidth = kIconWidth + kIconPadding; 

     _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kIconPadding, kIconPadding, kIconWidth, kIconWidth)]; 
     [self.contentView addSubview:_iconImageView]; 
    } 

    return self; 
} 
相关问题