I have an array of dictionary and i would like to get an item from it and use it

时间:2017-04-10 02:34:36

标签: ios arrays swift dictionary

var savedFaouritePlaces: NSMutableArray = [] //matched strings

Here is the info I get and I want to use this items cell for row at index path

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "HeartTabCell") as? HeartTabTableViewCell else {
        return UITableViewCell()
    }

    cell.heartTabTitleLabel.text = "Wanna use that title here"
    cell.heartTabImageView.image = "wanna use that image here" 

    return cell
}

2 个答案:

答案 0 :(得分:1)

You should use indexPath to retrieve each dictionary in that array:

let dict = savedFaouritePlaces[indexPath.row]

cell.heartTabTitleLabel.text = dict["Title"]
cell.heartTabImageView.image = UIImage(named: dict["Image"])

答案 1 :(得分:0)

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeartTabCell") as? HeartTabTableViewCell
        let dictionary = savedFaouritePlaces[indexPath.row]    
        cell.heartTabTitleLabel.text = dictionary["Title"]
        cell.heartTabImageView.image = UIImage(named: dictionary["Image"])
        return cell
    }