add image to uitableview cell

前端 未结 7 1266

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

7条回答
  •  庸人自扰
    2021-01-30 10:58

    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
        }
        

提交回复
热议问题