ios 将图像添加到 uitableview 单元格

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

add image to uitableview cell

iosuitableviewuikituiimage

提问by AMH

I have a tableview, how can I add image to the left of this cells?

我有一个tableview,如何在此单元格的左侧添加图像?

回答by André Moruj?o

cell.imageView.image = [UIImage imageNamed:@"image.png"];

UPDATE: Like Steven Fisher said, this should only work for cells with style UITableViewCellStyleDefault which is the default style. For other styles, you'd need to add a UIImageView to the cell's contentView.

更新:就像史蒂文·费舍尔所说,这应该只适用于样式为 UITableViewCellStyleDefault 的单元格,这是默认样式。对于其他样式,您需要将 UIImageView 添加到单元格的 contentView。

回答by Aman Aggarwal

Try this code:--

试试这个代码:--

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"arrow2.png"];
[cell addSubview:imv];
[imv release];

回答by Vladimir

Standard UITableViewCell already contains UIImageView that appears to the left to all your labels if its image is set. You can access it using imageView property:

标准 UITableViewCell 已经包含 UIImageView,如果设置了它的图像,它会出现在所有标签的左侧。您可以使用 imageView 属性访问它:

cell.imageView.image = someImage;

If for some reason standard behavior does not suit your needs (note that you can customize properties of that standard image view) then you can add your own UIImageView to the cell as Aman suggested in his answer. But in that approach you'll have to manage cell's layout yourself (e.g. make sure that cell labels do not overlap image). And do not add subviews to the cell directly - add them to cell's contentView:

如果由于某种原因标准行为不适合您的需求(请注意,您可以自定义该标准图像视图的属性),那么您可以按照 Aman 在他的回答中建议的那样将自己的 UIImageView 添加到单元格中。但是在这种方法中,您必须自己管理单元格的布局(例如,确保单元格标签不与图像重叠)。并且不要直接将子视图添加到单元格 - 将它们添加到单元格的 contentView:

// DO NOT!
[cell addSubview:imv]; 
// DO:
[cell.contentView addSubview:imv];

回答by Keith Holliday

For my fellow Swift users, here is the code you will need:

对于我的 Swift 用户,以下是您需要的代码:

let imageName = "un-child-rights.jpg"
let image = UIImage(named: imageName)
cell.imageView!.image = image

回答by Masih Sadri

Swift 4 solution:

斯威夫特 4 解决方案:

    cell.imageView?.image = UIImage(named: "yourImageName")

回答by saintjab

Swift 5 solution

斯威夫特 5 解决方案

cell.imageView?.image = UIImage.init(named: "yourImageName")

回答by vredrav

All good answers from others. Here are two ways you can solve this:

其他人的所有好答案。这里有两种方法可以解决这个问题:

  1. Directly from the code where you will have to programmatically control the dimensions of the imageview

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "xyz", for: indexPath)
        ...
        cell.imageView!.image = UIImage(named: "xyz") // if retrieving the image from the assets folder 
        return cell
    }
    
  2. From the story board, where you can use the attribute inspector & size inspector in the utility pane to adjust positioning, add constraints and specify dimensions

    • In the storyboard, add an imageView object in to the cell's content view with your desired dimensions and add a tag to the view(imageView) in the attribute inspector. Then do the following in your viewController

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "xyz", for: indexPath)
          ...
          let pictureView = cell.viewWithTag(119) as! UIImageView //let's assume the tag is set to 119
          pictureView.image = UIImage(named: "xyz") // if retrieving the image from the assets folder 
          return cell
      }
      
  1. 直接从代码中您必须以编程方式控制图像视图的尺寸

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "xyz", for: indexPath)
        ...
        cell.imageView!.image = UIImage(named: "xyz") // if retrieving the image from the assets folder 
        return cell
    }
    
  2. 在故事板中,您可以使用实用程序窗格中的属性检查器和大小检查器来调整定位、添加约束和指定尺寸

    • 在故事板中,将 imageView 对象添加到具有所需尺寸的单元格内容视图中,并在属性检查器中向视图(imageView)添加标签。然后在您的 viewController 中执行以下操作

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "xyz", for: indexPath)
          ...
          let pictureView = cell.viewWithTag(119) as! UIImageView //let's assume the tag is set to 119
          pictureView.image = UIImage(named: "xyz") // if retrieving the image from the assets folder 
          return cell
      }