【问题标题】:I have an array of dictionary and i would like to get an item from it and use it我有一个字典数组,我想从中获取一个项目并使用它
【发布时间】:2017-09-04 23:13:48
【问题描述】:
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
}

【问题讨论】:

标签: ios arrays swift dictionary


【解决方案1】:

您应该使用indexPath 来检索该数组中的每个字典:

let dict = savedFaouritePlaces[indexPath.row]

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

【讨论】:

    【解决方案2】:
        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
        }
    

    【讨论】:

    • 这解决了我的问题,谢谢大家让 dic:[String:Any] = savedFaouritePlaces[indexPath.row] as! [String : Any] print("savedFaouritePlaces: (dic["Description"])") cell.heartTabTitleLabel.text = dic["Title"] as!细绳? cell.heartTabImageView.image = UIImage(named: dic["Image"] as!String)
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2020-01-15
    • 2021-06-02
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多