UITableViewCell with image on the right?

前端 未结 11 1086
南方客
南方客 2020-12-13 05:48

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

11条回答
  •  孤城傲影
    2020-12-13 06:37

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

    This is how it might look like in code:

    MYTableViewCell.h:

    #import 
    
    @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
    

提交回复
热议问题