UITableViewCell中图片如何放在右侧?

54

有没有一种方法可以将UITableViewCell.image设置为在单元格的右侧显示,而不是左侧?或者我需要在单元格布局的右侧添加一个单独的UIImageView吗?

11个回答

93

不行,但你可以将一个图片视图作为表格单元格的附件视图来轻松实现相同的效果。

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

1
更改附加视图是否会覆盖披露指示器? - Oliver GL
是的,如果你想要这样做,那么你必须在创建时手动添加一个UIImageView到你的单元格中。或者为你的特殊表格单元格创建一个子类,将所有内容都包装得很好。 - Alex Wayne
UIImage方法中有一个小错别字:应该写成[UIImage imageNamed:@"foo.png"]。 - andyroberts
3
抱歉唤醒了这个帖子,但一开始它并没有起作用,我不得不在释放之前和设置accessoryview属性之后添加[imageView sizeToFit];,希望这能有所帮助 :) - Adam Purdie
1
通过对contentView应用变换,您可以将imageView放置在右侧。您还需要应用变换来翻转imageView和任何标签。 - TomSwift

12

对于简单的情况,你可以这样做:

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

这将翻转(镜像)内容视图,将任何imageView放在右侧。请注意,必须翻转imageView和任何文本标签,否则它们本身也会被镜像!此解决方案保留了右侧的附加视图。


8

这里是使用 accessoryView 方法的 Swift 示例:

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!
}

5

如果你不想创建自定义单元格并使用标准单元格进行工作,那么至少有两种方法:

  1. 使用accessoryView。
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
  1. 如果您想要正确的图像+附件视图,则可以将UIImageView输出口添加到ContentView(例如名称:rightImage),并将textLabel的背景颜色更改为Clear。
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()

4

Swift3Swift4中,我们可以使用以下代码:

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

2

您可以通过子类化UITableViewCell并覆盖layoutSubviews方法,然后调整self.textLabel、self.detailTextLabel和self.imageView视图的框架来实现自定义布局。

以下是代码示例:

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

有一种方法可以通过编程方式设置position。以下是解决方案的Swift版本:

image.frame = CGRect.init(
x: self.view.frame.width - image.frame.width*1.1,
y: image.frame.origin.y,
width: image.frame.width,
height: image.frame.height)

这将把您的图像定位在单元格的右侧。 该解决方案会根据屏幕大小自动调整位置。 唯一的缺点是当您旋转设备时,您需要重新加载数据,但您可以在您的UITableView类中覆盖didRotate监听器方法:

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    _tableView.reloadData()}

0

这个解决方案是为了在每个单元格中添加不同的图像...只是尝试

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if(indexPath.row == 0) {
    cell.image = [UIImage imageNamed:@"image.png"];
}

else if (indexPath.row == 1) {
    cell.image = [UIImage imageNamed:@"image1.png"];
}

不相关于问题? - Alasdair Allan
OP已经知道如何使用单元格的图像视图添加图像,这就是这个片段展示的全部内容。如何将图像定位到右侧才是问题。 - andyroberts

0

要在单元格的右侧添加图像,我建议您创建一个带有右侧图像视图的自定义单元格,这对您非常有用。并添加一个空视图以将此nib与customcell类链接。

现在,在您的单元格的nib文件中删除视图,添加tableViewcell,并在该单元格中拖放一个imageView到右侧。

现在进入TableViewCell的类DemoCell,创建一个outlet并将其设置为tablecell的nib中的imageview。

现在,在tableview的cellforrowatindexpath方法中引用您的单元格,并像这样使用它与nib名称一起。

static NSString *CellIdentifier = @"Cell";
DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell = [[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
}

根据您的要求设置图像

cell.imageVw.img ....


0

没有必要创建自己的图像,只需编辑cell.tintColor


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接