Reputation: 93
I am pretty new to iOS-development, I hope that you can help me.
I have a dict where the keys present the sections and the array for each key represents the rows. I am trying to edit the cell in a way, that each cell in its section corresponds to one element of the array.
var contacts = [String: [Contact]]()
This is my dict and Contact is a custom class with attributes like firstName and lastName.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let keyArray = Array(contacts.keys)
let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath)
cell.textLabel?.text =
return cell
}
I'd like to have the contact's lastName show up as the text for the cell.textLabel.
Thank you already for taking your time to help me. I appreciate this a lot.
Raph
Upvotes: 0
Views: 1417
Reputation: 285059
section
contains an integer number. You have to create a relation between the dictionary keys and the section number for example
let sectionArrayKeys = ["contactsA", "contactsB"]
Now get the array for the section
let sectionKey = sectionArrayKeys[indexPath.section]
let contactSection = contacts[sectionKey]
and the appropriate contact for the row
let contact = contactSection[indexPath.row]
A more suitable solution – as mentioned in the comments – is a nested array
var contacts = [[Contact]]()
...
let contactSection = contacts[indexPath.section]
let contact = contactSection[indexPath.row]
Upvotes: 1