0

So I was wondering if it is possible to display a small image in a detailTextLabel of a cell? (I'm getting the image from a URL) I tried it with:

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

Which for obvious reason doesn't work. I also tried with:

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

Which also (obviously) doesn't work, because I'm passing an image to a string parameter.

Surprisingly I didn't find anything on the WWW. (Search parameters were: "image in detailTextLabel" or picture or just textlabel and image and various variations)

Blade
  • 1,435
  • 1
  • 14
  • 22

3 Answers3

2

You can't put it directly into the label because labels only display text. But you can use the imageView property of the UITableViewCell.

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

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

Letrstotheprez
  • 622
  • 1
  • 5
  • 11
  • Well yes, but then it gets displayed on the left, I was hoping I could display it in the detailTextLabel. – Blade Mar 05 '12 at 22:38
  • I guess, but I was hoping for a better solution. Thanks though. – Blade Mar 05 '12 at 22:42
  • You could always make your own UIImageView and add it to the cell as a subview. You won't be able to set an image in a UILabel though. – Letrstotheprez Mar 05 '12 at 22:43
1

Rather than putting the image in the imageView property, you could create an UIImageView and place it in the accessoryView property:

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;

This will size the image to fit within the cell and position it to the right of the textLabel and detailTextLabel.

David M. Syzdek
  • 15,360
  • 6
  • 30
  • 40
1

A better approach is not to rely on the built-in UITableViewCell types and create your own cell frame and add all the labels, view, images at your desired position inside the cell. Then you can return the cell for your tableview.

greenhorn
  • 1,097
  • 6
  • 10