Generic UITableView

This is a fairly general question, I've read a lot of answers, but I'm still wondering how this should be done correctly.

When you have a table view similar to the settings in iOS, where you have cells with a list of tables with many different (in terms of type) cells (some with UISwitche

, with UIPickerViews

, for more details, with UITextField

s, etc.), should it be done with static tableView

or dynamic

? And what's the best way to go about achieving something like this?

And I'm asking for cases where information should be retrieved from a backend like a database Firebase

.

+3


source to share


4 answers


You have to create custom cells in the table view and then you need to define the type of cell you want to use and after that set the cells.

Suppose you have three types of cells:

  • Cell c Switch

    (regular)
  • Cell c textField

    (regular)
  • Regular cell (regular)


In yours, cellForRowAt

you need to define which cell you want to use. You can do this by checking indexPath.row

if you have static positions, or you can check the data type in your array and set the cell depending on that.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    switch indexPath.section {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchCell", for: indexPath) as! SwitchCell
        // set cell values here
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell", for: indexPath) as! TextFieldCell
        // set cell values here
        return cell
    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UITableViewCell
        // set cell values here
        return cell
    }
}

      

Here is a good guide to creating custom cells and you can have as many custom cells as you like.

+4


source


It just depends on your needs,

  • If you are going to render UI elements like UITextField

    , UISwitch

    etc. based on a certain condition or from the api response tool, you can go from dynamic tableView

    . In a dynamic tableView, you can create multiple cells with different IDs or create tableViewCell

    in nib

    .
  • If you will always show the same interface elements, you can go with static tableView

    .


If you want to show UI elements based on the response from firebase database

, you can go with dynamic tableView

. Then only you will have control over the process and your data.

Thank.

0


source


I have done the same many times in the past.

You can include information in the data that allows the table to remove the desired cell type and fill it.

For example, you might have a dictionary like ...

[
    "type" : "Switch",
    "on" : true,
    "text" : "Allow location access"
]

      

Now in your table view you can include type

in the dictionary to get the cell type you want. Then you can make sure the radio button isOn

and set the shortcut text to Allow location access

.

Of course, you will need a lot more than just this particular case, but the same basic principle applies.

0


source


You have already answered your own question.

1. You receive data from Firebase. Then it can probably be dynamic. Then it's better to go with dynamic proto-type cells.

2. So the data will not be the same. Then create separate prototype cells for each type. Finally, use "Key" based prototype cells in dynamic data.

Even your Firebase data is Static above is the best option.

0


source







All Articles