It would be more convenient, you create a structured data first to compose this your table, because so it is easy for you to structure and edit your data when necessary.
Then, whenever you go to use sections and cells of a UITableView
, you better at all times use a two-dimensional array instead of a dictionary.
I’ll give you a very simple example of how this structure would look in your case, anyway, it’s simple enough for just about any other case where you need to use sections and Cells.
First see these structs:
struct Section {
var title: String = ""
var items: [ SectionItem ] = []
}
struct SectionItem {
var name: String = ""
var value: String = "Some Value"
}
They simply serve to structure the data you need to display on the screen, it helps you better organize your code, more flexibility for future changes and there are also other advantages that don’t need to be mentioned now but that you may notice over time.
Ok, now with the data structured and ready to be loaded, you can already use this structure to compose a UITableView
very simply.
Consider the following implementation:
struct ViewModel {
var sections: [ Section ] = [] // Preencha essa array da forma que quiser
}
class ViewController: UITableViewDataSource, UITableViewDelegate {
var viewModel = ViewModel()
func numberOfSections(in tableView: UITableView) -> Int {
return viewModel.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.sections[ section ].items.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return viewModel.sections[ section ].title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SomeIdentifier")!
let data = viewModel.sections[ indexPath.section ].items[ indexPath.row ]
cell.textLabel?.text = data.name
cell.detailTextLabel?.text = data.value
return cell
}
}
If you were to wear one Dictionary
, you would probably have to map the data to get the right Dice, the way above, you take the data for the section and the cell with its respective Dice, because you provide exactly the amount of sections and cells you need.
Well I hope it helps.
I’m sorry but I don’t understand. I’m new to Swift. I have 2 info:
arrayJogador: [String]
andarrayQuadra: [String]
. In your proposal hastitle
,items
,name
,value
. The sections are in thearrayQuadra
and the lines of the sections are inarrayJogador
.– Adriano Chedid
In my proposal, you will use a
struct
to define everything related to table session data,title
for example, it would be the name that appears in the section, you can add as many as you want, because your session is now a struct.items
is an array of elements that fill the cells of each session, being also astruct
, you will have more control of the information and will have a more readable code as well. Try doing it with an isolated example first, it’s the best thing to understand this approach.– Jan Cássio