user3324594
user3324594

Reputation: 49

Pulling Static Images From an Array

I'm new to coding so hope the below makes sense!

I have an array in Xcode that is placed in a file called 'numbersData'.

I am using a reusuable table cell elsewhere and want to pull in information from this array for each cell based on its index path.

I have no trouble doing this for text....However i'm finding it difficult to populate a UIImage view in the cell.

My array has the name of the image in it, using the identifier "badge". I have saved the images in images.xcassets.

Here is the code I have in my table view controller:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("StoryCell") as NumbersTableViewCell

    cell.titleLabel.text = numbersData[indexPath.row]["title"] as? String
    cell.titleLabel2.text = numbersData[indexPath.row]["title2"] as? String
    cell.summaryLabel.text = numbersData[indexPath.row]["subtitle"] as? String

    cell.articleImageView.image = //This is where I am stuck!
    cell.delegate = self

    return cell
}

My array looks like this:

[
    "id": 1,
    "title": "BODMAS - The Order Of",
    "title2": "Things",
    "subtitle": "orem ipsum and all that malarky and not of the hipster variety",
    "segue": "NumbersSegue1",
    "badge": "cat-pic"
],
[
    "id": 2,
    "title": "Rearranging & Solving",
    "title2": "Equations",
    "subtitle": "orem ipsum and all that malarky and not of the hipster variety",
    "segue": "NumbersSegue2",
    "badge": "numbersPicture2"
],

Any help would be amazing!!!

Upvotes: 0

Views: 90

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236350

Try like this:

cell.articleImageView.image = UIImage(named: (numbersData[indexPath.row]["badge"] as? String)! )

Upvotes: 1

Related Questions