0

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!!!

1 Answer 1

1

Try like this:

cell.articleImageView.image = UIImage(named: (numbersData[indexPath.row]["badge"] as? String)! )
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! It looks good but I keep getting a weird error saying that "value of optional type 'string' not unwrapped and asking me if I meant to use '?' or '!'....this seems weird to me as a question mark is being used!
I have edited dictionary returns an optional so you need to unwrap it before casting
var numbersData: [[String:AnyObject]] = [ [ "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" ], etc
Thank you! I changed it slightly to this to include the indexPath.row and it worked! - cell.articleImageView.image = UIImage(named: (numbersData[indexPath.row]["badge"] as? String)! )
kkk I just forgot to replace the 0 I was using here to test it without a tableView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.