Is it possible to align a UITableViewCell image to the right?

when adding an image to a table cell, by default it goes to the left:

cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];

how can I change it so that each image in the UITableViewCell automatically switches to the right and the textLabel is 10 pixels to the left of the image.

Thanks a lot!

+5
source share
6 answers

Another way is to create a custom cell and override the method layoutSubviews.

@interface CustomCell : UITableViewCell

@end

@implementation CustomCell

- (void)layoutSubviews
{
   [super layoutSubviews];

   // grab bound for contentView
   CGRect contentViewBound = self.contentView.bounds;
   // grab the frame for the imageView
   CGRect imageViewFrame = self.imageView.frame;
   // change x position
   imageViewFrame.origin.x = contentViewBound.size.width - imageViewFrame.size.width;
   // assign the new frame
   self.imageView.frame = imageViewFrame;
}

@end

Enter what cellForRowAtIndexPathyou need to create and reuse in CustomCell, not UITableViewCell.

Hope this helps.

Edit

#import "CustomCell.h"

// other code here...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    return cell;
}
+10
source

Find the solution code here.

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

For reference.

UITableViewCell with image on the right?

+5

:

cell.imageView.frame = CGRectMake(cell.frame.size.width - cell.imageView.frame.size.width, cell.imageView.frame.origin.y, cell.imageView.frame.size.width, cell.imageView.frame.size.height);
[cell.yourTexLabel sizeToFit];
cell.yourTexLabel.frame = CGRectMake(cell.imageView.origin.x - cell.yourTexLabel.frame.size.width - 10, cell.yourTexLabel.frame.origin.y, cell.yourTexLabel.frame.size.width, cell.yourTexLabel.frame.size.height);
+1

@TomSwift fooobar.com/questions/128461/...

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

contentView, .

+1

UITableViewCell. :

  • objective-C, UITableViewCell, LabeledImageTableViewCell. ivars UILabel UIImageView.
  • Interface Builder UITableView Dynamic Prototypes. a UIImageView a UILabel . LabeledImageTableViewCell. UILabel UIImageView LabeledImageTableViewCell.
  • In the delegate, for UITableView(usually a UITableViewController, sometimes a UIViewController) implements data source methods, for example:

    //#pragma mark - Table view data source
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return (NSInteger)[rowData count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"tvcLabeledImage";
        LabeledImageTableViewCell *cell = (LabeledImageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[LNCCorrelationInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.myImage = [UIImage imageNamed:@"imageForThisRow.png"];
        cell.myLabel = "imageForThisRow";
        return cell;
    }
    

Also, check out Apple videos from WWDC 2011, UITableView Changes, Tips, and Tricks and Getting to Know the Interface Builder Builder (Login required: https://developer.apple.com/videos/wwdc/2011/ .)

0
source

in swift3 and swift4, we can use this:

cell.accessoryView = UIImageView (image: UIImage (named: "imageNmae")!)

0
source

All Articles