Reputation: 564
I know that there is this topic : "Mixing static and dynamic sections in a grouped table view"
But after many hours, I still don't have finished this litle "project" :
To do that : click there to see a screen capture (It's two grouped style : on the first group the cells are in a dynamic style and the other the cell is in a static style.)
So, I have a list :
var listOfWords = ["Cat", "Dog", "Horse", "Dolfin", "Robot"]
And I would display each word in each cell -> so it is dynamic...
Then In the other group, down I would like to have a "cell-button" where if I click, I could create other word that would be added in the list.
So if someone can help me, it will be helpful ! (please send the code ;-) )
Upvotes: 1
Views: 397
Reputation: 861
You need to have two sections and manage them differently on the cellForRowAtIndexPath
datasource method.
Add two sections on the numberOfSections
datasource method
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
Manage the numberOfCell
datasource method
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return yourArray.count
case 1:
return 1
default:
return 0
}
}
Manage each sections accordingly. Based on what you said, you want the first section to be dymanic and the second static.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
// your dynamic cells
case 0:
let dynamicCell = tableView.dequeueReusableCell(withIdentifier: "dynamicCell") != nil ? tableView.dequeueReusableCell(withIdentifier: "dynamicCell")! : UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "dynamicCell")
dynamicCell.textLabel?.text = yourArray[indexPath.row]
return dynamicCell
// your static cell
case 1:
guard let staticCell = tableView.dequeueReusableCell(withIdentifier: "staticCell") else {
let staticCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "staticCell")
staticCell.textLabel?.text = "Static"
return staticCell
}
staticCell.textLabel?.text = "Static"
return staticCell
default:
return UITableViewCell()
}
}
Hope this helps.
Also, there might some minor mistakes with my datasource methods name
Upvotes: 1
Reputation: 562
Put the button in a tableFooterView.
https://github.com/dpfannenstiel/symmetrical-octo-journey
Upvotes: 0