add image to uitableview cell

IosUitableviewUikitUiimage

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

Try this code:--

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

Solution 3 - Ios

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:

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:

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

Solution 4 - Ios

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

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

Solution 5 - Ios

Swift 4 solution:

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

Solution 6 - Ios

Swift 5 solution

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

Solution 7 - Ios

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
         }
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAMHView Question on Stackoverflow
Solution 1 - IosAndré MorujãoView Answer on Stackoverflow
Solution 2 - IosAman AggarwalView Answer on Stackoverflow
Solution 3 - IosVladimirView Answer on Stackoverflow
Solution 4 - IosKeith HollidayView Answer on Stackoverflow
Solution 5 - IosMasih SadriView Answer on Stackoverflow
Solution 6 - IossaintjabView Answer on Stackoverflow
Solution 7 - IosvredravView Answer on Stackoverflow