UITableViewCell with image on the right?
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:
- To use accessoryView.
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
- 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")!)
Oliver GL
Updated on July 09, 2022Comments
-
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 about 15 yearsWill changing the accessory view then overwrite the disclosure indicator?
-
Alex Wayne about 15 yearsYeah, 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 almost 13 yearsNot relevant to the question?
-
arooaroo almost 13 yearsThe 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 almost 13 yearsThere's a small typo in the UIImage method: it should say [UIImage imageNamed:@"foo.png"]
-
Adam Purdie over 12 yearsSorry 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 almost 9 yearsBy 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.