UITableViewCell with image on the right?

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

+41
objective-c iphone uitableview uiimage uiimageview
Apr 23 '09 at 5:16
source share
9 answers

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

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]]; cell.accessoryView = imageView; [imageView release]; 
+71
Apr 23 '09 at 17:12
source share
— -

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, placing any image to the right. Please note that you need to flip imageView and any text labels, otherwise they themselves will be mirrored! This solution retains the appearance of the accessories on the right.

+10
Jul 24 '15 at 17:44
source share

Here is an example in Swift 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! } 
+4
Apr 25 '15 at 23:53 on
source share

If you do not want to create a custom cell and work with the standard one, you have at least two ways:

  • Use an accessory.
 cell.accessoryView = UIImageView(image: UIImage(named: "imageName")) cell.accessoryView.frame = CGRectMake(0, 0, 22, 22) 
  1. If you want to have the correct image + accessory, you can add the UIImageView output to the ContentView (example name: rightImage) and change the background color of the textLabel to Clear.
 cell.rightImage.image = UIImage(named: "imageName") cell.textLabel.backgroundColor = UIColor.clearColor() 
+4
Jul 30 '15 at 5:39
source share

Subclass UITableViewCell and override layoutSubviews, then simply adjust the frames of the self.textLabel, self.detailTextLabel and self.imageView views.

Here's what it might look like in code:

MYTableViewCell.h :

 #import <UIKit/UIKit.h> @interface MYTableViewCell : UITableViewCell @end 

MYTableViewCell.m :

 #import "MYTableViewCell.h" @implementation MYTableViewCell - (void)layoutSubviews { [super layoutSubviews]; if (self.imageView.image) { self.imageView.frame = CGRectMake(CGRectGetWidth(self.contentView.bounds) - 32 - 8, CGRectGetMidY(self.contentView.bounds) - 16.0f, 32, 32); CGRect frame = self.textLabel.frame; frame.origin.x = 8; self.textLabel.frame = frame; frame = self.detailTextLabel.frame; frame.origin.x = 8; self.detailTextLabel.frame = frame; } } @end 
+2
Apr 17 2018-12-12T00:
source share

This Soln is designed to add another image to each cell .... Just try

 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } if(indexPath.row == 0) { cell.image = [UIImage imageNamed:@"image.png"]; } else if (indexPath.row == 1) { cell.image = [UIImage imageNamed:@"image1.png"]; } 
0
Mar 01 '10 at 19:05
source share

To add an image @ The right side in the cell, I would suggest you create a custom cell with the image on the right side, this will be very useful for you. and add an empty view to associate this nib with the customcell class.

Now, in your nib element of the cell file, delete the view, add tableViewcell and drag the image to the right in this cell.

Now go to the TableViewCell class, say DemoCell, create a socket and install it using the image in the nib tabletop

Now in the tableview method cellforrowatindexpath refer to your cell and use it with a nib name like this

 static NSString *CellIdentifier = @"Cell"; DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell = [[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject]; } 

and set the image according to your requirement

cell.imageVw.img ....

0
Jul 12 '12 at 9:45
source share

You can resize the image in the accessory by calling the setFrame method of the image this way: [imageView setFrame: CGRectMake (0, 0, 35.0, 35.0)];

0
Jul 22 '13 at 19:31
source share

No need to create your own image, just edit cell.tintColor .

0
Jul 14 '15 at 7:49
source share



All Articles