1

我正在为我的视图实现一个表视图控制器。作为苹果文档中的具体细节,我UITableViewCellStyleSubtitle在表格中使用了我的单元格。

我需要的是一个单元格,左侧包含一个小头像,粗体textLabel和较小的detailTextLabel. 两者textLabeldetailTextLabel都是多行,而不仅仅是一行。

我正在尝试链接中的教程,但模拟器只显示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, newsDescriptionfor的内容textDetailLabel。它还没有包含头像。如果有人可以帮助我解决此问题并将较小的头像添加到此表格视图单元格中,我将不胜感激。

4

1 回答 1

0

您应该创建一个自定义UITableViewCell子类,该子类具有 a 的属性,您可以使用您在 中选择的UIImageView来填充该属性。我建议将其添加为的子视图,然后将单元格的缩进调整为加上一些填充的宽度。这具有将单元格的全部内容向右移动以为图像腾出空间的效果:UIImagecellForRowAtIndexPath:UIImageViewcontentViewUIImageView

- (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;
}
于 2013-04-24T01:24:27.380 回答