UITableViewCell with image on the right?

47,338

Solution 1

No. But you can easily add an image view as the accessory view to a table cell for the same effect.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];

Solution 2

For simple cases you can do this:

cell.contentView.transform = CGAffineTransformMakeScale(-1,1);
cell.imageView.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.textAlignment = NSTextAlignmentRight; // optional

This will flip (mirror) the content view placing any imageView on the right. Note that you have to flip the imageView and any text labels also otherwise they themselves would be mirrored! This solution preserves the accessory view on the right.

Solution 3

Here is an example in Swift with the approach of using accessoryView:

private let reuseIdentifier: String = "your-cell-reuse-id"

// MARK: UITableViewDataSource

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell
  if (cell == nil) {
    cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:reuseIdentifier)
  }

  cell!.textLabel!.text = "Hello"
  cell!.detailTextLabel!.text = "World"
  cell!.accessoryView = UIImageView(image:UIImage(named:"YourImageName")!)
  return cell!
}

Solution 4

If you don't want to make a custom cell and will work with standard one, you have as minimum two ways:

  1. To use accessoryView.
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
  1. If you want to have the right image + accessoryView, then you can add UIImageView outlet to ContentView (example name: rightImage) and change background color of textLabel as Clear.
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()

Solution 5

in swift3 and swift4, we can use this:

cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
Share:
47,338
Oliver GL
Author by

Oliver GL

Updated on July 09, 2022

Comments

  • Oliver GL
    Oliver GL almost 2 years

    Is there a way to set the UITableViewCell.image to display on the right hand side of the cell instead of the left? Or will I need to add a separate UIImageView on the right side of the cell layout?

  • Oliver GL
    Oliver GL about 15 years
    Will changing the accessory view then overwrite the disclosure indicator?
  • Alex Wayne
    Alex Wayne about 15 years
    Yeah, it would. If you want that then you have to manually add a UIImageView to your cell on creation. Or create a subclass for your special table cell that wraps that all up nicely for you.
  • rafaedez
    rafaedez almost 13 years
    Not relevant to the question?
  • arooaroo
    arooaroo almost 13 years
    The OP already understands how to add an image using the cell's image view, which is all this snippet shows. How to position an image to appear on the right-side is the question.
  • arooaroo
    arooaroo almost 13 years
    There's a small typo in the UIImage method: it should say [UIImage imageNamed:@"foo.png"]
  • Adam Purdie
    Adam Purdie over 12 years
    Sorry for awakening this thread but it didn't work off the startline, i had to add [imageView sizeToFit]; before the release and after setting the accessoryview property, hope this helps :)
  • TomSwift
    TomSwift almost 9 years
    By applying a transform on the contentView you can place the imageView on the right. You also ned to apply a transform to flip the imageView and any labels back.