【问题标题】:How do I use key value pairs from a dictionary to feed table view cells their title?如何使用字典中的键值对为表格视图单元格提供标题?
【发布时间】:2020-05-30 13:37:54
【问题描述】:

我目前正在尝试完成我的第一个应用程序的收尾工作。作为我正在学习的课程的一部分,通过 Xcode 快速编写,我有一个出现在表格视图中的图像列表,我想以编程方式更改文件的名称,使其不显示 IMG_xxxxx.jpg .我创建了一个字典来将每张照片与所需的名称相关联,但是我无法弄清楚如何使表格视图显示一个单元格标题,该单元格标题是所述字典中的键的值,基于哪个文件被加载到任何给定的单元格中。

//This is the dictionary 
let imageTitles = ["IMG_1085.JPG" : "Num Num Num",
                   "IMG_1251.JPG" : "Star of the Show",
                   "IMG_1643.JPG" : "Ante Up",
                   "IMG_1630.jpg" : "Someone called my name?",
                   "IMG_1340.JPG" : "Ahhhhhhhhh",
                   "IMG_1595.jpg" : "Focus",
                   "IMG_1689.JPG" : "He Likes Me",
                   "IMG_1676.jpg" : "Warm and Cozy",
                   "IMG_1688.JPG" : "It's Like Looking Into a Mirror...",
                   ]

这些是加载单元名称的位。一个被注释掉了(这是教练教的“官方”代码,为了学习,我试图更进一步),另一个是我自己完成这项任务的尝试(我没有保存每一个尝试-我只是在不成功时用新的东西重写。)当我决定添加此功能时,另一条评论更像是给自己的一个注释。我想我有可能在错误的地方尝试这样做,但我认为情况并非如此。抛出的错误是说当需要 int 时我不能有 string:string 引用,这是有道理的(从逻辑上讲,我假设 .row 将是一个 int 值,但我不知道如何发表声明要求一个字符串:字符串值,我相信这会解决我的问题)。

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
    //cell.textLabel?.text = pictures[indexPath.row]
    cell.textLabel?.text = imageTitles[indexPath.row]
    //this is where I need to make the reference of the dictonary

    return cell
}

最后,如果我只是不知道如何编写需要 string:string 的参数是对的,有没有什么方法可以解决这个问题?我尝试在此类网站上查找现有线程,但没有任何运气。

提前感谢您的帮助!

【问题讨论】:

  • 我想是这样,但是通过代码,而不是明确的陈述?我认为这就是我想要做的概念;使用图像文件名来触发它在字典中的值被显示。 'var pictures = [String]()' 这是我对类顶部图片的引用,但它是空的,直到稍后的 for 循环循环(我认为)应用程序中的所有内容,如果任何给定信息的前缀是“IMG”,循环将其附加到图片变量中。使用任何给定元素引用字典的代码(或方法?请更正我的词汇)是什么?

标签: ios swift xcode uitableview dictionary


【解决方案1】:

您可以将它们视为 元组数组 快速:

let imageTitles: [(imageName: String, imageTitle: String)] = [
    ("IMG_1085.JPG", "Num Num Num"),
    ("IMG_1251.JPG", "Star of the Show"),
    ("IMG_1643.JPG", "Ante Up"),
    ("IMG_1630.jpg", "Someone called my name?"),
    ("IMG_1340.JPG", "Ahhhhhhhhh"),
    ("IMG_1595.jpg", "Focus"),
    ("IMG_1689.JPG", "He Likes Me"),
    ("IMG_1676.jpg", "Warm and Cozy"),
    ("IMG_1688.JPG", "It's Like Looking Into a Mirror..."),
]


 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
    cell.textLabel?.text = imageTitles[indexPath.row].imageTitle
    return cell
}

为了更好地管理它们,如果可以更改,只需将其更改为特定类型的数组,例如:

struct Image {
    var imageName: String
    var imageTitle: String
}

let imageTitles: [Image] = [
    Image(imageName: "IMG_1085.JPG", imageTitle: "Num Num Num"),
    ...
]

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
    cell.textLabel?.text = imageTitles[indexPath.row].imageTitle
    return cell
}

【讨论】:

    【解决方案2】:

    你需要一个数组作为数据源,简单的例子是一个文件名数组

    var pictures = ["IMG_1085.JPG", "IMG_1251.JPG", "IMG_1643.JPG", "IMG_1630.jpg", "IMG_1340.JPG", "IMG_1595.jpg", "IMG_1689.JPG", "IMG_1676.jpg", "IMG_1688.JPG"]
    

    cellForRow中获取索引路径的文件名并映射到标题

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
        let fileName = pictures[indexPath.row]
        cell.textLabel?.text = imageTitles[fileName] ?? "N/A"
        return cell
    }
    

    更好的解决方案是将自定义结构作为模型,其中包括文件名和人类可读的描述。

    【讨论】:

    • 成功了!缺少的部分是使用单独的代码行来引用字典,然后使用该常量将人名插入到文本中。非常感谢您的帮助,在学习本课程时,我会牢记结构解决方案,寻找一个可以帮助我了解如何使用该选项的时刻。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    相关资源
    最近更新 更多