你能在detailTextLabel单元格中显示一个小图像吗?

时间:2012-03-05 22:09:06

标签: ios cell tableview image detailtextlabel

所以我想知道是否可以在细胞的detailTextLabel中显示一个小图像? (我从URL获取图像)我尝试了:

NSString *thumbs = [NSString stringWithFormat:@"%@", TableText3];
cell.detailTextLabel.text = thumbs;

由于显而易见的原因不起作用。我也尝试过:

UIImage *thumbs = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                          [NSURL URLWithString:TableText3]]];
cell.detailTextLabel.text = thumbs;

其中(显然)也不起作用,因为我将图像传递给字符串参数。

令人惊讶的是,我没有在WWW上找到任何东西。 (搜索参数是:“image in detailTextLabel”或图片或只是textlabel和图像以及各种变化)

3 个答案:

答案 0 :(得分:2)

您无法将其直接放入标签中,因为标签只显示文字。但是您可以使用UITableViewCell的imageView属性。

cell.imageView = [[UIImageView alloc] initWithImage: thumbs];

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html

答案 1 :(得分:1)

您可以创建一个UIImageView并将其放在accessoryView属性中,而不是将图像放在imageView属性中:

UIImage     * thumbs;
UIImageView * thumbsView;
CGFloat       width;

thumbs             = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                              [NSURL URLWithString:TableText3]]];
thumbsView         = [[[UIImageView alloc] initWithImage:thumbs] autorelease];
width              = (cell.frame.size.height * thumbs.size.width) / thumbs.size.height;
thumbsView.frame   = CGRectMake(0, 0, width, cell.frame.size.height);
cell.accessoryView = thumbsView;

这将调整图像大小以适合单元格并将其定位在textLabel和detailTextLabel的右侧。

答案 2 :(得分:1)

更好的方法是不依赖于内置的UITableViewCell类型并创建自己的单元框架,并在单元格内的所需位置添加所有标签,视图和图像。然后,您可以返回您的tableview的单元格。