简体   繁体   English

如何使用字典中的键值对为表格视图单元格提供标题?

[英]How do I use key value pairs from a dictionary to feed table view cells their title?

I'm currently trying to complete the finishing touches on my first app.我目前正在尝试完成我的第一个应用程序的最后润色。 Written in swift via Xcode as part of a course I'm taking, I've got a list of images that appear in a table view, and I want to programmatically alter the names of the files so it doesn't display IMG_xxxxx.jpg.作为我正在学习的课程的一部分,通过 Xcode 快速编写,我有一个显示在表格视图中的图像列表,我想以编程方式更改文件的名称,使其不显示 IMG_xxxxx.jpg . I created a dictionary to associate each photo with the desired name, but I cannot figure out how to make the table view display a cell title that is the value of a key in said dictionary based on which file gets loaded in any given cell.我创建了一个字典来将每张照片与所需的名称相关联,但我无法弄清楚如何让表格视图显示一个单元格标题,该标题是根据在任何给定单元格中加载的文件,该字典中的键的值。

//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...",
                   ]

These are the bits that load the name of a cell.这些是加载单元名称的位。 One is commented out (that's the 'official' code taught by the instructor, I'm trying to take it one step further for learning's sake) and the other is my own attempts at completing this task (I've not saved each and every try- I just rewrite with something new when unsuccessful.) The other comment was more a note to self when I decided to add this feature.一个被注释掉了(这是老师教的“官方”代码,为了学习,我试图更进一步),另一个是我自己完成这项任务的尝试(我没有保存每一个尝试 - 我只是在不成功时用新的东西重写。)当我决定添加此功能时,其他评论更像是对自己的提醒。 I suppose it is possible I'm trying to do this in the wrong spot, but I don't think that is the case.我想我有可能在错误的地方尝试这样做,但我认为情况并非如此。 The error thrown is saying that I can't have a string:string reference when int is expected, and that makes sense (logically, I would assume .row would be an int value, but I don't know how to make a statement that asks for a string:string value, which I believe would solve my problem).抛出的错误是说当需要 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
}

Lastly, if I'm right about just not knowing how to write a parameter that needs string:string, is there any way to have figured that out burdened with inexperience?最后,如果我只是不知道如何编写需要 string:string 的参数是正确的,那么有没有办法在缺乏经验的情况下解决这个问题? I tried looking for existing threads on sites like this but without any luck.我尝试在这样的网站上寻找现有线程,但没有任何运气。

Thanks in advance for any assistance!在此先感谢您的帮助!

You need an array as data source, simple example an array of the file names您需要一个数组作为数据源,简单的例子是一个文件名数组

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"]

In cellForRow get the file name for the index path and map it to the titlecellForRow获取索引路径的文件名并将其映射到标题

 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
}

A better solution is a custom struct as model which includes both the file name and the human readable description.更好的解决方案是自定义结构作为模型,其中包括文件名和人类可读的描述。

You could treat them as an array of tuples as a quicky:您可以将它们视为快速的元组数组

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
}

In future to better manage them, if you can change it, just change it as an array of a specific type something like:将来为了更好地管理它们,如果您可以更改它,只需将其更改为特定类型数组,例如:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM