0

I have created a table view and set text to the cells. I have aligned text to right. I am trying to set image in the cell as shown in the snapshot below. enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
dip
  • 3,548
  • 3
  • 24
  • 36
  • Add more detail. How are you creating your cells? CustomTableViewCells? Are you creating them programmatically? or through storyboard? – Shamas S Mar 19 '15 at 09:22
  • [This will help you a bit](http://stackoverflow.com/questions/19318421/how-to-embed-small-icon-in-uilabel) – Bista Mar 19 '15 at 09:24
  • @iphonic Yes sir #iosDev82 yes , i am on programatic way! – dip Mar 19 '15 at 09:26

3 Answers3

1

You can do it with constraints in your custom UITableViewCellclass. Here is a code snippet:

@implementation CustomTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];

    // Initialization code
    [self setupSubviews];
}

- (void)setupSubviews {
    UIImageView *imageView = [[UIImageView alloc] init];
    [self.contentView addSubview:imageView];
    [imageView setTranslatesAutoresizingMaskIntoConstraints:NO];

    // You can set your image here
    [imageView setImage:yourImage];

    UILabel *label = [[UILabel alloc] init];
    [self.contentView addSubview:label];
    [label setTranslatesAutoresizingMaskIntoConstraints:NO];
    [label setTextAlignment:NSTextAlignmentRight];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[image]-[label]-0-|" options:0 metrics:nil views:@{ @"label":label, @"image":imageView }]];
    [imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[label]-0-|" options:0 metrics:nil views:@{ @"label":label }]];

    [self.contentView layoutIfNeeded];
}

@end

Let me know if you have questions and if it works out.

Catalina T.
  • 3,456
  • 19
  • 29
0

UITableViewCell has a property imageView, where you can set a thumbnail image that appears on the left side, using the default UITableViewCellStyle.

You can set it on your tableView:cellForRowAtIndexPath method:

cell.imageView.image = UIImage(named: "image.png")

If you are using a custom cell, you'll need to add a UIImageView to your cell.

If what you are trying to archive are the cells from the bottom of your drawing, then you'll need to add the UIImageView to your custom cell and use some constraints or Auto Layout to align in to the text.

0

My suggestion would be to create YourTableViewCell and add constraints so everything is aligned to the right(there are plenty of tutorials for creating custom UITableViewCell).

The UITableViewCell image is always on the left so I think that won't be your solution.

Another thing you could do is to calculate location and create image in cellAtIndexPath and add it to the cell.

All in all, I would use custom cell.

Miknash
  • 7,888
  • 3
  • 34
  • 46