How to assemble a dictionary from two arrays, one from keys and the other from Swift values

Asked

Viewed 186 times

1

I have two arrays containing information of courts and players. Need to put in a tableview this data, being that the sections of the table would be the blocks and the values, of each section, would be the players. The sections can have 2 or 4 lines each. This will depend on what is selected in a segmetedControl.

  1. arrayQuadra: String
  2. arrayJewer: String

I thought of creating a dictionary [String: [String]] but I’m not familiar with this popular dictionary.

1 answer

0

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] and arrayQuadra: [String]. In your proposal has title, items,name, value. The sections are in the arrayQuadra and the lines of the sections are in arrayJogador.

  • 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 a struct, 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.