0

I am trying to code images to UIImage but I am having trouble. I am not sure how code it for each different image per list item. The app I am building is a List with pictures attached to each list item.

Below is the MyWhatsit class:

class MyWhatsit { var name: String { didSet { postDidChangeNotification() } } var location: String { didSet { postDidChangeNotification() } } var image: UIImage? { didSet { postDidChangeNotification() } } var viewImage: UIImage { return image ?? UIImage(named: "camera")! } init( name: String, position: String = "" ) { self.name = name self.location = location } func postDidChangeNotification() { let center = NSNotificationCenter.defaultCenter() center.postNotificationName(WhatsitDidChangeNotification, object: self) } } 

Below is the Table View:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return things.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) let thing = things[indexPath.row] as MyWhatsit cell.textLabel?.text = thing.name cell.detailTextLabel?.text = thing.location cell.imageView?.image = thing.viewImage return cell } override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { things.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } 
4
  • 1
    Kindly do not vandalize your posts. Commented Apr 20, 2016 at 20:56
  • I would like to delete my post. Commented Apr 20, 2016 at 20:57
  • 6
    @DavidSmith You can raise a moderator flag on your question to alert a moderator. But vandalizing it is not the solution. Write why you want to delete your post inside the flag. Commented Apr 20, 2016 at 20:59
  • @DavidSmith Now that you've vandalized it again, an automatic flag has been raised and a moderator will come and look at your situation soon. Please stop removing all the content of your post in the mean time. Commented Apr 20, 2016 at 21:11

3 Answers 3

3

I believe all you need to do is create an array of images like you are with the title and subtitle. When you add another picture, add the image name to the array and append the tableView.

What I see in your code is a name and position. It looks like you need to add "image" and set it to the name of the image. For example, MyWhatsit(name: "Bob Smith", position: "midfielder", image: "bobSmithImage")... and then you set the cell's image view equal to the image name.

Hope this gets you moving!

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your feedback. I added some of my code.
No problem! What I see in your code is a name and position. It looks like you need to add "image" and set it to the name of the image. For example, MyWhatsit(name: "Bob Smith", position: "midfielder", image: "bobSmithImage") ...and then you set the cell's image view equal to the image name.
Im getting an error for the image view equal to the image name. Can you give me an example code?
Could you show me what code you used and the error that Xcode is giving you?
0

You need to create your own personal category. For example sorting your players based on positions they play. Using a class that stores your images into an array. For example:

let goalkeeper = [UIImage(named:"ivanlucic"), UIImage(named:"manuelneuer")] let rightBack = [UIImage(named:"danialves"), UIImage(named:"sergioramos")] 

After you have a list of images, inside your cellForRowAtIndexPath you can do a checking, and reference it to the array your created.

2 Comments

Thank you for your suggestion. I added some of my code, does it change your suggestion?
Structure is up to you how you want to do. You may do it this way too, but I would suggest that you change your image file names to the same as the name of that player, so that when referencing, there will be lesser codes to write.
0

Didn't get how you are setting the code in the tableview but you can add image name with names like

Where image key contains the image name you have stored in the XCAssets

var things: [MyWhatsit] = [ MyWhatsit(name: "Ivan Lucic", position: "Goalkeeper","image":"ivan"), MyWhatsit(name: "Manuel Neuer", position: "Goalkeeper","image":"manuel")...] 

And you can init the image at the same time you init the name

init( name: String, position: String = "", imagename:string ) { self.name = name self.position = position self.imgview = UIImage(named:imagename) } 

1 Comment

Thank you. I added the Table View Code. Does that change the code you recommended?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.