Reputation: 23
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. 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).
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? I tried looking for existing threads on sites like this but without any luck.
Thanks in advance for any assistance!
Upvotes: 1
Views: 694
Reputation: 6547
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
}
Upvotes: 1
Reputation: 285180
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 title
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.
Upvotes: 1