Reputation: 3637
This is rather generic question, I've read many answers, but I'm still wondering how it should be done properly.
When you have a table view that's like the Settings in iOS, where you have a table view cells with many different (in terms of type) cells (some with UISwitche
, with UIPickerViews
, for more details, with UITextField
s, etc) should it be done using a static tableView
or a dynamic
? And what is the best way to go for achieving something like that?
And I'm asking for cases, where the information should be pulled from a backend like Firebase
database for example.
Upvotes: 3
Views: 709
Reputation: 3013
You gave already answer to your question.
1.You are getting data from Firebase. Then probably it could be dynamic.Then better to go with dynamic proto type cells.
2.So the data won't be same.Then make separate prototype cells for each type.Finally use the prototype cells based on 'Key' in dynamic data.
Even your Firebase data is Static above way is the best one.
Upvotes: 0
Reputation: 77661
I've done the same thing many times in the past.
You can include information in the data that will allow the table to dequeue the correct kind of cell and populate it.
For instance you might have a dictionary something like...
[
"type" : "Switch",
"on" : true,
"text" : "Allow location access"
]
Now in your table view you can switch on the type
in the dictionary to get the right type of cell. Then you can make sure the switch isOn
and set the label text to Allow location access
.
Of course, you'll need a lot more than just this particular case but the same basic principle applies.
Upvotes: 0
Reputation: 38843
You should create custom cells in your table view and then you need to detect which type of cell you want to use and after that set cells.
Let´s say that you have three type of cells:
Switch
(custom)textField
(custom)In your cellForRowAt
you need to detect which kind of cell you want to use. You can either do that by checking the 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 of how you can create your custom cells and you can have as many custom cells as you want.
Upvotes: 4
Reputation: 2505
Simply it depends on your needs,
UITextField
, UISwitch
, etc.. based on certain condition or from api response means you can go with dynamic tableView
. In dynamic tableView you can design multiple cells with different identifier or you can create tableViewCell
in nib
.static tableView
.If you want to show UI elements based on response from firebase database
means, you can go with dynamic TableView
. Then only you will have control on you process and your data.
Thanks.
Upvotes: 0