Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an Image into a cell

Tags:

I have read heaps of questions but can't seem to find the answer

I have a TableView but can't add a image to my cell

cell.imageView.image = UIImage(named: "osx_design_view_messages") 

This code should work because I basically copied it from a answer to a question but for some reason it isn't working. Any suggestions?

Im using swift by the way

 override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {     let cellIdentifier = "cell"      var cell : UITableViewCell      cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell     cell.textLabel.text = self.recipies[indexPath.row]     var image : UIImage = UIImage(named: "osx_design_view_messages")     return cell } 
like image 528
Johno2110 Avatar asked Jun 07 '14 06:06

Johno2110


2 Answers

Here are a few possible causes of your issue:

  1. The cell prototype needs to include a UIImageView. If you're using a storyboard select the cell prototype and make sure its style is set to "Basic" in the attributes inspector (right panel).

  2. The cell identifier doesn't match the identifier typed in for your prototype cell in your storyboard. Make sure that "cell" is entered for "Identifier" also in the attributes inspector.

  3. The image is not loading from file properly. To test this, add the following line after initializing your image:

    println("The loaded image: \(image)")

After checking that everything is set in your storyboard, try running it using this code:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!     {         let cellIdentifier = "cell"          var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell          cell.textLabel.text = self.recipes[indexPath.row]          var image : UIImage = UIImage(named: "osx_design_view_messages")         println("The loaded image: \(image)")         cell.imageView.image = image          return cell     } 
like image 63
Dash Avatar answered Oct 03 '22 20:10

Dash


Swift 3

// we are using this if your images are at server.

// we are getting images from a url.

// you can set image from your Xcode.

// it's doing using assign a tag to imageView

  1. The URL of images are in an array name = thumbnail i.e self.thumbnail[indexPath.row]
  2. on UITableviewCell put a imageView on cell
  3. select UIimageView assign it a tag from storyboard.

    let pictureURL = URL(string: self.thumbnail[indexPath.row])! let pictureData = NSData(contentsOf: pictureURL as URL) let catPicture = UIImage(data: pictureData as! Data) var imageV = UIImageView() imageV = cell?.viewWithTag(1) as! UIImageView imageV.image = catPicture 
like image 44
Ameer Chand Avatar answered Oct 03 '22 20:10

Ameer Chand