How to assemble a dictionary from two arrays, one from keys and another from swift values

1

I have two arrays containing information about blocks and players. I 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. Sections can have 2 or 4 rows each. This will depend on what is selected in a segmetedControl .

  • arrayQuadra: String
  • arrayPlayer: String
  • I thought about creating a [String: [String]] dictionary but I'm not aware of this dictionary.

        
    asked by anonymous 02.11.2016 / 16:55

    1 answer

    0

    It would be more convenient, you create a structured data first to compose this table, because it makes it easy for you to structure and edit your data when needed.

    Then, whenever you're going to use sections and cells of a UITableView , you'd better always use a two-dimensional array rather than a dictionary.

    I'll give you a very simple example of how this structure would look for your case, anyway, it's simple enough for just about any other case where you need to use sections and cells.

    First look at 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, this helps you organize your code better, gives you more flexibility for future changes, and there are other advantages that you do not need to mention right now but maybe you'll notice time.

    Ok, now with the data structured and ready to load, you can already use this structure to compose a UITableView quite 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 use a Dictionary , you would probably have to map the data to get the right index, as above, you get the data for the section and the cell with its index, because you provide the exact amount of sections and cells you need.

    I hope this helps.

        
    06.11.2016 / 01:50