objective-c UITableViewCell 右侧有图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/780395/
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-03 21:26:26  来源:igfitidea点击:

UITableViewCell with image on the right?

iphoneobjective-cuitableviewuiimageviewuiimage

提问by Oliver GL

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 side of the cell layout?

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

回答by Alex Wayne

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

回答by TomSwift

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.

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

回答by Zorayr

Here is an example in Swiftwith the approach of using accessoryView:

这是Swift 中使用方法的示例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!
}

回答by gerram

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.
  1. 使用附件视图。
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.
  1. 如果您想拥有正确的图像 + 附件视图,那么您可以将 UIImageView 插座添加到 ContentView(示例名称:rightImage)并将 textLabel 的背景颜色更改为清除。
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()

回答by Camsoft

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

子类化 UITableViewCell 并覆盖layoutSubviews,然后调整 self.textLabel、self.detailTextLabel 和 self.imageView 视图的框架。

This is how it might look like in code:

这是它在代码中的样子:

MYTableViewCell.h:

MYTableViewCell.h

#import <UIKit/UIKit.h>

@interface MYTableViewCell : UITableViewCell
@end

MYTableViewCell.m:

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

回答by Mohsen mokhtari

in swift3and swift4, we can use this:

swift3swift4 中,我们可以使用这个:

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

回答by iCoder

To Add image @ Right Hand side in the Cell, I would suggest you to create a Custom cell with imageview on right hand side , it will be very useful for you . and add an empty view to link this nib with customcell class.

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

Now in your cell's nib file remove the view,add tableViewcell and in that cell drag & drop an imageView to right hand side.

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

Now go to the TableViewCell's class say DemoCell , create an outlet and set it with the imageview in the tablecell's nib

现在转到 TableViewCell 的类说 DemoCell ,创建一个插座并使用 tablecell 笔尖中的 imageview 设置它

Now,in tableview's cellforrowatindexpath method reference your cell and use it with the nib name like this

现在,在 tableview 的 cellforrowatindexpath 方法中引用您的单元格并将其与笔尖名称一起使用,如下所示

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

and set the image as per your requirement

并根据您的要求设置图像

cell.imageVw.img ....

cell.imageVw.img ....

回答by user2205998

You can resize image in the accessoryview by calling setFrame method of imageview like this: [imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)];

您可以通过调用 imageview 的 setFrame 方法来调整附件视图中的图像大小,如下所示: [imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)];

回答by user283846

This Soln is for adding different image in each cell....Just a Try

这个 Soln 用于在每个单元格中添加不同的图像......只是尝试

// 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"];
}

回答by EranKT

There is a way to set the positionprogrammatically. Here is the Swiftversion of the solution:

有一种以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)

This will position your image on the right side of the cell. This solution is adjusting automatically the position according to screen size. The only down side is when you rotate the device you need to reload your data, but you can just overridein your UITableViewclass the didRotatelistener method:

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

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