Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell image alignment to the right, possible?

when adding an image to table cell, as default it goes to left:

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

how can I change it that every image in the UITableViewCell will go automaticlly to the right and the textLabel will be 10px to the left of the image.

Thanks alot!

like image 916
ytpm Avatar asked Feb 25 '12 16:02

ytpm


4 Answers

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

@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

Rembember that in cellForRowAtIndexPath you need to create and reuse CustomCell and not UITableViewCell.

Hope it 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;
}
like image 171
Lorenzo B Avatar answered Sep 22 '22 05:09

Lorenzo B


Find the solution here code.

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

For your reference.

UITableViewCell with image on the right?

like image 22
Narasimha Nallamsetty Avatar answered Sep 19 '22 05:09

Narasimha Nallamsetty


try this:

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);
like image 42
meronix Avatar answered Sep 20 '22 05:09

meronix


Found a better answer from @TomSwift here https://stackoverflow.com/a/31616694/1884707

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

By applying a transform on the contentView you can place the imageView on the right.

like image 36
abi Avatar answered Sep 22 '22 05:09

abi