xcode UITableViewCell 图像向右对齐,可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9445702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:26:27  来源:igfitidea点击:

UITableViewCell image alignment to the right, possible?

iphoneobjective-ciosxcodeuitableview

提问by Yossi Tsafar

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.

我该如何更改 UITableViewCell 中的每个图像都会自动向右移动,而 textLabel 将在图像左侧 10px 处。

Thanks alot!

非常感谢!

回答by Lorenzo B

Another way is to create a custom cell and override layoutSubviewsmethod.

另一种方法是创建自定义单元格和覆盖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

Rembember that in cellForRowAtIndexPathyou need to create and reuse CustomCelland not UITableViewCell.

请记住,cellForRowAtIndexPath您需要创建和重用CustomCell而不是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;
}

回答by Narasimha Nallamsetty

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?

UITableViewCell 右侧有图像?

回答by abi

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

在这里从@TomSwift 找到了更好的答案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.

通过在 contentView 上应用转换,您可以将 imageView 放在右侧。

回答by Mohsen mokhtari

in swift3 and swift4, we can use this:

在 swift3 和 swift4 中,我们可以使用这个:

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

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

回答by meronix

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);

回答by paul.lander

One solution is to use a custom UITableViewCell. The steps are:

一种解决方案是使用自定义UITableViewCell. 步骤是:

  1. Create a new objective-C class that is a subclass of UITableViewCell, for example LabeledImageTableViewCell. Declare ivarsand propertiesfor a UILabeland a UIImageView.
  2. In Interface Builder, set the contentof the UITableViewto Dynamic Prototypes. Drag a UIImageViewand a UILabelto a table view cell and position them. Set the cell's class to LabeledImageTableViewCell. Connect the outlets of the cell to the UILabel& UIImageViewobjects of LabeledImageTableViewCell.
  3. In the delegate for UITableView(usually a UITableViewController, sometimes a UIViewController) implement the datasourcemethods, 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;
    }
    
  1. 创建一个新的 Objective-C 类,它是 的子类UITableViewCell,例如LabeledImageTableViewCell。声明a和 a 的ivars属性UILabelUIImageView
  2. 在Interface Builder中,设定的内容UITableView,以动态原型。将 aUIImageView和 a拖到UILabel表视图单元格并放置它们。将单元格的类设置为LabeledImageTableViewCell. 将单元格的出口连接到 的UILabel&UIImageView对象LabeledImageTableViewCell
  3. 在 for UITableView(通常是 a UITableViewController,有时是 a UIViewController)的委托中实现数据源方法,例如:

    //#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 the Apple videos from WWDC 2011, UITableView Changes, Tips & Tricksand Introducing Interface Builder Storyboarding(Login required: https://developer.apple.com/videos/wwdc/2011/.)

此外,请查看 WWDC 2011 的 Apple 视频、UITableView Changes、Tips & TricksIntroducing Interface Builder Storyboarding(需要登录:https: //developer.apple.com/videos/wwdc/2011/。)