Regarding TableViews: How can I manage section titles and rows through classes? - ios

So I have been studying Swift and trying to use a TableView with two sections. The thing is:
I have successfully developed an application using TableViewController with just one section and used data from a class called "Opcao" to populate the rows.
So I decided to create another section by setting return 2 on override func numberOfSections(in tableView: UITableView) -> Int and it worked, I only really needed two sections.
My problem: both of the sections are presenting the same number of rows and the same content on it. How could I change it? I mean, I would like the second section called "Teste" to have its own cell fields (different from the first section) but also populated with info of Opcao class.
The sections names on my TableView should be actually the attribute called "section", and the rows content should be the the number of rows in a cell is how many objects there are with which kind of "section". What should I do?
Opcao.swift:
class Opcao {
var nome:String
var descricao: String
var section: String
var segueIdentifier: String
init(nome: String, descricao: String, section: String, segueIdentifier: String){
self.nome = nome //displayed as main value of the cell
self.descricao = descricao //description which goes bellow the cell title
self.section = section // what I though it could be the section tittle which the option belongs to
self.segueIdentifier = segueIdentifier //used for other stuff, not relevant to this situation
}
Parts of TableViewController.swift:
class TableViewController: UITableViewController {
var opcoes: [Opcao] = []
var titulos: [String] = ["1a Habilitação", "Teste"]
override func viewDidLoad() {
super.viewDidLoad()
gerarOpcoes()
}
func gerarOpcoes(){
//criando opcao 1
var opcao1: Opcao
opcao1 = Opcao(nome: "Novo simulado", descricao: "Clique para começar um novo simulado.", section: "phab", segueIdentifier: "A")
self.opcoes.append(opcao1)
//criando opcao 2
var opcao2: Opcao
opcao2 = Opcao(nome: "Responder livremente", descricao: "Responda diversas perguntas sem tempo limite.", section: "phab", segueIdentifier: "B")
self.opcoes.append(opcao2)
//criando opcao 3
var opcao3: Opcao
opcao3 = Opcao(nome: "Histórico", descricao: "Veja seus últimos resultados.", section: "phab", segueIdentifier: "C")
self.opcoes.append(opcao3)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return opcoes.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "celula", for: indexPath)
cell.textLabel?.text = self.opcoes[indexPath.row].nome
cell.detailTextLabel?.text = self.opcoes[indexPath.row].descricao
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return titulos[section]
}

You can do it in a variety of ways. Easiest way would be having different arrays for different sections (Although it might not be the best approach). Then altering numberofRowsInSection depending on that too. Lets see:
Create another array:
var opcoesSecond: [Opcao] = []
Another deployment method for second array, this time lets put two objects only:
func gerarOpcoesForSecond(){
var opcao1: Opcao
opcao1 = Opcao(nome: "Novo simulado", descricao: "Clique para começar um novo simulado.", section: "phab", segueIdentifier: "A")
self.opcoesSecond.append(opcao1)
//criando opcao 2
var opcao2: Opcao
opcao2 = Opcao(nome: "Responder livremente", descricao: "Responda diversas perguntas sem tempo limite.", section: "phab", segueIdentifier: "B")
self.opcoesSecond.append(opcao2)
}
Call both array deployment methods in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
gerarOpcoes()
gerarOpcoesForSecond()
}
Then in your numberofRowsInSection method:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return opcoes.count
} else {
return opcoesSecond.count
}
}
In your cellForRowAt method:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "celula", for: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = self.opcoes[indexPath.row].nome
cell.detailTextLabel?.text = self.opcoes[indexPath.row].descricao
} else {
cell.textLabel?.text = self.opcoesSecond[indexPath.row].nome
cell.detailTextLabel?.text = self.opcoesSecond[indexPath.row].descricao
}
return cell
}
Again as mentioned in the comments, two-dimensional array might be better to prevent code repetition like we have in cellForRowAt method.
But this should solve your problem.

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return opcoes.count
}
you should return count based on the section.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "celula", for: indexPath)
cell.textLabel?.text = self.opcoes[indexPath.row].nome
cell.detailTextLabel?.text = self.opcoes[indexPath.row].descricao
return cell
}
again, you should set cell based on the section.

Related

organizing dates in uitableview sections

I don't know where to start...
I need a TableView to show Information, organized by dates.
I created a Class "Model" to hold the data...
class Model: NSObject {
var start: Date?
var str: String?
override init()
{
}
init(start: Date, str: String) {
self.start = start
self.str = str
}
Creating Elements of that Class
let value1 = Model(start: Date1(), str: string1)
let value2 = Model(start: Date2(), str: string2)
Filling an Array of that Elements:
var array = [Model]()
array.append(value1, value2)
Populating the TableView
How can I divide the array, for example into months, or workweeks and so on...
I want the tableView to organize the data in sections !?
func numberOfSections(in tableView: UITableView) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = "January", Febuary, e.g.
return label
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ?
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TC_Arbeitszeit
cell.label.text = ?
return cell
}
Any help is greatly appreciated !
Use the dictionary group by functionality. You can organize your array to keys and values, the key will be your sections and the values will be your raw.
example for grouping by day:
extension Date {
var day: Int? {
let components = Calendar.current.dateComponents([.day], from: self)
return components.day
}
}
let array = [model1, model2]
let dict = Dictionary(grouping: array) { (model) -> Int in
return model.start?.day ?? 0
}
for the example let say that the day of "start" parameter on model1 is Sunday
and the day of "start" parameter on model2 is Monday
so the dict will group it like this:
[1: [model1], 2: [model2]]
now you can use the key as section and the values as rows
func numberOfSections(in tableView: UITableView) -> Int {
return dict.keys ?? 0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let day = dic.keys[section]
let label = UILabel()
label.text = String(day)
return label
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let key = dict[section]
return dict[key].count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TC_Arbeitszeit
let data = dict[indexPath.section]
cell.label.text = String(data[indexPath.row].start.day)
return cell
}
Right now your mode is a simple array. Since you want groupings, your model needs to change a little, so that it includes sections/groups. You might start with an dictionary of arrays, where the keys are, say, the month or workweek.
Have a look at Swift's reduce:into: where you can iterate over your existing array and break it down into such a dictionary.
Then sort the dictionary's keys into a 'sections' array. The count of this is your table view's section count. So now your model has a sections array, and a dictionary, dateInfo.
When the table view asks for a row, look up the key in the sections array, let key = sections[indexPath.section], then find the model item itself:
var dateInfo: [Date: [Model]]
var sections: [Date] // sorted
...
let sectionContent = dateInfo[key] as! [Model]
// rowCount is just sectionContent.count
let rowInfo = sectionContent[indexPath.row]
// populate cell...
Hopefully that's enough of a pointer to get you headed in the right direction.
Based on your case, generating a 2d array would be an appropriate choice.
How to do it?
let transfomredArray = Array(Dictionary(grouping: array, by: { $0. start! }).values)
thus transfomredArray is an array of arrays, each array in should contain models with same date.
Therefore you can handle your tableview data source method based on that, example:
func numberOfSections(in tableView: UITableView) -> Int {
return transfomredArray.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
let date = transfomredArray[section][0].start!
label.text = "\(date)"
return label
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return transfomredArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TC_Arbeitszeit
cell.label.text = transfomredArray[indexPath.section][indexPath.row]
return cell
}

Adding two Custom cells in tableView

I have a tableView on mainStoryboard with two custom cells.
I would like to set two more cells at different row.
However When I implemented the code the added cells replaces original cells. (Custom cell of "Basic grammar3" and "Basic grammar5" are disappearing.)
I was trying to find the answer but could not find out.
I have image and code added below.
import UIKit
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tblStoryList: UITableView!
var array = PLIST.shared.mainArray
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 || indexPath.row == 3 || indexPath.row == 5 {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
cell.headerTitle.text = indexPath.row == 0 ? "First Stage" : indexPath.row == 3 ? "Second Stage" : "Third Stage"
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "StoryTableviewCell", for: indexPath) as! StoryTableviewCell
//making plist file
let dict = self.array[indexPath.row - 1]
let title = dict["title"] as! String
let imageName = dict["image"] as! String
let temp = dict["phrases"] as! [String:Any]
let arr = temp["array"] as! [[String:Any]]
let detail = "progress \(arr.count)/\(arr.count)"
//property to plist file をつなぐ
cell.imgIcon.image = UIImage.init(named: imageName)
cell.lblTitle.text = title
cell.lblSubtitle.text = detail
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
return
}
tableView.deselectRow(at: indexPath as IndexPath, animated:true)
if indexPath.row == 3 {
return
}
tableView.deselectRow(at: indexPath as IndexPath, animated:true)
if indexPath.row == 5 {
return
}
tableView.deselectRow(at: indexPath as IndexPath, animated:true)
let messagesVc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
messagesVc.object = self.array[indexPath.row - 1]
self.navigationController?.show(messagesVc, sender: self)
}
You could use sections for your table view. Now, you are returning 1 in your numberOfSections function. And it is creating only one section. If you want to use headers, you can use sections for your need. And also you can fill your table view cells with multidimendional arrays. For example:
For adjusting your section headers:
let lessonTitles = ["First Stage", "Second Stage"]
Titles for sections:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < lessonTitles.count {
return lessonTitles [section]
}
return nil
}
For adjusting your sections and rows:
let lessons = [["Basic Grammar 1", "Basic Grammar 2"], ["Basic Grammar 3", "Basic Grammar 4"]]
Number of sections function should be:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return lessons.count
}
Number of rows in section should be:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lessons[section].count
}
And creating your cells is like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellText = data[indexPath.section][indexPath.row]
...
}
Try like this...
func numberOfSections(in tableView: UITableView) -> Int
{
return numberOfStages
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numberOfRowsInCurrentStage
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
return customizedCell
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
return requiredHeight
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
{
return stageCountView
}
You can use viewForHeaderInSection if you want to show stage count on top.
edit: The comment by raki is the much better solution (use headers). I leave this here in case you want something closer to your existing implementation.
You have to change your numbering scheme in order to insert these additional rows (and not replace existing rows). So you might want to adjust the row for the "normal" elements like this:
func adjustRow(_ row: Int) -> Int {
if row < 3 {
return row
} else if row < 5 {
return row+1
} else {
return row+2
}
}

UITableView not showing all sections

I have a problem that I want to show a tableview, but separated in sections by the "status" of each item. I know how to do it with a simple string array, but I can't get to make this work with a class (Aluno) array, here's my code so far:
import UIKit
class DeliveriesTVC: UITableViewController {
let sections = ["Delivered", "Not Delivered"]
var studentList: [Array<Student>] = [[], []]
override func viewDidLoad() {
super.viewDidLoad()
for i in 0...5{
studentList[0].append(Student(alunoNome: "Aluno \(i)", alunoImg: "dani_test", alunoStatus: "Delivered"))
}
for i in 6...10{
studentList[1].append(Student(alunoNome: "Aluno \(i)", alunoImg: "dani_test", alunoStatus: "Not Delivered"))
}
self.title = "Deliveries"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cellEntrega = tableView.dequeueReusableCell(withIdentifier: "EntregaCell", for: indexPath) as? EntregaCell {
let entregaCell = studentList[indexPath.section][indexPath.row]
// here i call a function in my TableViewCell class that update the cell itself
cellEntrega.updateAlunoUI(Aluno: entregaCell)
return cellEntrega
} else {
return UITableViewCell()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listaAlunos[section].count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
}
In the output, i just get the "first section" showing, and without a name, even with me setting the name of each section and the number of sections. I've looked everywhere but i couldn't find a solution.
Your numberOfSections and titleForHeader methods are wrong, it should be
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
Additionally, you should return self.sections.count instead of return 2 in numberOfSections being hardcoded as in case you add another object to the array, you will have to change the 2 to whatever elements the array has now.
For your numberOfSectionInTableView function, shouldn't it be override func numberOfSectionsInTableView(in tableView: UITableView) -> Int {
return 2
} with a in in front of the tableView: UITableView?
I don't see where you connect your UITableView's delegate and datasource.
Here is a tutorial to show your about using UITableView.
Take a look at "Basic Table View" - "Step 3: Set the datasource and delegate"

How to add rows to specific section in tableview with dictionary?

I have a dictionary and importing information from my database. I need to put it specifically in the right section in my table view. All the information is provided, if you need more detail or code I will provide it.
Dictionary output ["March 27": ["do the dishes", "take out the trash"], "March 29": ["Walk the dog", "Water the plants"], "March 28": ["Clean the house"]]
var date = ["March 27", "March 28", "March 29"]
func numberOfSections(in tableView: UITableView) -> Int {
return date.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return date[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! Chores
//Having trouble on what to do here
return cell
}
That's how you can do, I think this is self explanatory:
var output = ["March 27": ["do the dishes", "take out the trash"], "March 29": ["Walk the dog", "Water the plants"], "March 28": ["Clean the house"]]
var date = Array(output.keys)
func numberOfSections(in tableView: UITableView) -> Int {
return date.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return date[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return output[date[section]]?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! Chores
var value = output[date[indexPath.section]]?[indexPath.row] ?? ""
cell.textLabel.text = value
return cell
}
// Edited in number of rows you have:
output[date[section]]?.count
it's exactly like this, this mostly gives you optionals but I will ignore it in this example:
let keyForSection = date[section]
let arrayOfStringsForKey = output[keyForSection]
let numberOfRows = arrayOfStringsForKey.count
you do similar stuff to get the actual value but instead of count you pass index of the row you want the value from
let value = arrayOfStringsForKey[rowNumber]

How can I group TableView items from a dictionary in swift?

Lets consider this example:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return ???
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return names.count
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
return ???
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return ????
}
}
let's assume that we need that the keys (fruits and vegetables) of the dictionary are the number of sections, plus they will be the titles of the sections. The items of the keys (eg apples and banana) will be the rows of each section. How can I implement this in my code? I know it might be easy but I couldn't figure it out my self.
You can use struct for that and here is example:
import UIKit
class TableViewController: UITableViewController {
var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectArray = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
for (key, value) in names {
println("\(key) -> \(value)")
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectArray.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray[section].sectionObjects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
}
Swift 2
you dictionary example
var dic:Dictionary<String,String> = ["key":"value","key1":"value2"]
Your table
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
var key = Array(self.dic.keys)[indexPath.row]
var value = Array(self.dic.values)[indexPath.row]
cell.text = key + value
}
If you want it sorted use the global sorted function to sort the dictionary.
import UIKit
class TableViewController: UITableViewController {
var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]
var namesSorted = [String, Array<String>]()
override func viewDidLoad() {
super.viewDidLoad()
// Sort names
namesSorted = sorted(names) { $0.0 < $1.0} // namesSorted = ["Fruits": ["Apple", "Banana"], "Vegetables": ["Tomato", "Potato", "Lettuce"]]
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return namesSorted.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesSorted[section].1.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel?.text = namesSorted[indexPath.section].1[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return namesSorted[section].0
}
}
All collection types must be Array
var names = [["Tomato", "Potato", "Lettuce"], ["Apple", "Banana"]]
var sectionNames = ["Vegetables", "Fruits"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return names[section].count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return names.count
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
return sectionNames
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return sectionNames[section]
}
From Apple Documentation :
var keys: LazyForwardCollection<MapCollectionView<Dictionary<Key, Value>, Key>> { get }
Description: A collection containing just the keys of self. Keys appear in the same order as they occur as the .0 member of key-value pairs in self. Each key in the result has a unique value.
names.keys.array returns an Array of the keys.
SO:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return names.keys.array[section].count
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
return names.keys.array
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return names.keys.array[section]
}
This will work on Any Dictionary with any amount of data(even if it is unknown to the programmer
An easier way to solve this problem is to copy your dictionary into a temporary variable. Use removeFirst to extract the values from the array inside the dictionary.
var itemList=["Grocery":["soap","flour","carrots"],"Vehicles":["oil change","gas","tire rotation"],"Household":["Cable","Tv","cellphone"]]
var itemListTmp :[String:[String]] = [:]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text=itemListTmp[keysItem[indexPath.section]]?.removeFirst()
//cell.textLabel?.text=itemList[indexPath.section].items[indexPath.row]
return cell
}
Another way of solving this problem is to extract keys and values in separate arrays:
var task=[String](itemList.keys)
var tobeDone=[[String]](itemList.values)
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return task[section]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text=tobeDone[indexPath.section][indexPath.row]
return cell
}
Similar to https://stackoverflow.com/a/31136537/11098567 answer I would use classes instead of structs, so that you can manipulate or add to your values after it has been placed into the array.
#objc func addToInitialClassInstance() {
let classInstance = Class(property1: String, property2: [CLass2.init(property1: String, property2: String)])
let isAvailable = initialClassInstance.contains { (classInArray) -> Bool in
if classInArray.property == classInstance.property {
classInArray.property2.append(classInstance.property2[0])
return true
}
return false
}
if !isAvailable {
initialClassInstance.append(classInstance)
}
tableView.reloadData()
}

Resources