【问题标题】:UItable View cell with textLabel , detailTextLabel and UIImage带有 textLabel 、 detailTextLabel 和 UIImage 的 UItable 视图单元格
【发布时间】:2012-10-09 10:18:58
【问题描述】:

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

我需要的是一个单元格,其中包含左侧的小头像、粗体 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保留textLabelnewsDescription的内容为textDetailLabel。它还没有包含头像。如果有人可以帮助我解决此问题并将较小的头像添加到此表格视图单元格中,我将不胜感激。

【问题讨论】:

  • 你也必须在cellForRowAtIndexPath方法中设置textLabel和detailTextLabel的高度..
  • 你有没有得到解决方案?

标签: ios objective-c uiimageview uitableview


【解决方案1】:

您应该创建一个自定义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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多