【问题标题】:UITableViewCell image alignment to the right, possible?UITableViewCell 图像向右对齐,可能吗?
【发布时间】:2012-02-25 16:19:16
【问题描述】:

向表格单元格添加图像时,默认向左:

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

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

非常感谢!

【问题讨论】:

  • 恐怕你不能使用标准UITableViewCell 你必须使用自己的单元格然后自定义它。

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

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

请记住,在cellForRowAtIndexPath 中,您需要创建和重用CustomCell 而不是UITableViewCell

希望对你有帮助。

编辑

#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;
}

【讨论】:

  • 感谢您的解决方案。 PS:不是 imageViewFrame.position.x 而是 imageViewFrame.origin.x
  • 很好的解决方案!我已使用您的代码使图像居中
  • 你能分享一下你的 swift 变体吗?
【解决方案2】:

在此处找到解决方案代码。

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

供您参考。

UITableViewCell with image on the right?

【讨论】:

  • 感谢 Narasimha Nallamsetty ;)
【解决方案3】:

试试这个:

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

【讨论】:

  • 不,伙计...它不工作。另外,'yourTexLabel' 是 'cell.textLabel,但由于某种原因他无法识别它..
  • 哦,伙计...我只是猜到您正在使用自己的单元子类...您不能对标准单元做太多...
【解决方案4】:

从@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;

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

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

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

      1. 创建一个新的objective-C 类,它是UITableViewCell 的子类,例如LabeledImageTableViewCell。为 UILabelUIImageView 声明 ivarsproperties
      2. 在 Interface Builder 中,将 UITableViewcontent 设置为 Dynamic Prototypes。将UIImageViewUILabel 拖动到表格视图单元格并定位它们。将单元格的类设置为LabeledImageTableViewCell。将单元格的出口连接到LabeledImageTableViewCellUILabelUIImageView 对象。
      3. UITableView(通常是UITableViewController,有时是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;
        }
        

      另外,请查看 WWDC 2011 中的 Apple 视频,UITableView 更改、提示和技巧Introducing Interface Builder Storyboard(需要登录:https://developer.apple.com/videos/wwdc/2011/。) p>

      【讨论】:

        猜你喜欢
        • 2018-07-13
        • 2012-02-19
        • 2014-10-28
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多