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 }
Here are a few possible causes of your issue:
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).
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.
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 }
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With