David Smith
David Smith

Reputation: 11

How to load images from Images.xcassets into a UIImage

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.
    }
}

Upvotes: 0

Views: 281

Answers (3)

Dan Levy
Dan Levy

Reputation: 4281

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!

Upvotes: 3

HardikDG
HardikDG

Reputation: 6122

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)
}

Upvotes: 0

jo3birdtalk
jo3birdtalk

Reputation: 616

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.

Upvotes: 0

Related Questions