UITableViewCell with image on the right?

前端 未结 11 1069
南方客
南方客 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:22

    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];
    
    0 讨论(0)
  • 2020-12-13 06:22

    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!
    }
    
    0 讨论(0)
  • 2020-12-13 06:24

    It's unnecessary to create your own image, just edit cell.tintColor.

    0 讨论(0)
  • 2020-12-13 06:29

    If you don't want to make a custom cell and will work with standard one, you have as minimum two ways:

    1. To use accessoryView.
    cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
    cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
    
    1. 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()
    
    0 讨论(0)
  • 2020-12-13 06:31

    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.

    0 讨论(0)
  • 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 <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
    
    0 讨论(0)
提交回复
热议问题