iPhone SDK: How to create a dynamic cell/UITable with different uicontrols
In my iOS 4 universal app, I need to show a form-like page in the begining of my app. Depending on the xml I receive, I may need to ask users their name, gender, birth of date. etc. I can't know exactly what I will receive. I will create some logic how to interpret those "UI hints" sent by the server.
The problem is how can I reflect this in my UITableView, I can create a custom cell and send it some parameters from my plain UITableView for each question, that what kind of UI controls and which layout should it show?
A scenario:
I _may_
receive 6 questions to be asked, 3 of them are selection type questions like yes/no(I will prefer to use a table view checkmarkaccesory here not picker or segmented) the other three questions are name, surname and gender question which are separete questions actually and I want to treat them as seperate questions but show them as one question to the user and show them in one cell 3 small texfields horizontally.
Is that a good approach or possible to make a custom cell which is totally dynamic and creates itself by receiving parameters? Also I dont wanna end up with a messy custom cell code. Or can I create开发者_开发技巧 3-4 different types of custom cells and be able to mix and show them horizontally. like a grid?
Any suggestions?
There are multiple approaches to this issue and it solely depends on you which one you're comfortable with. Yet, I would suggest you to subclass UITableViewCell
for each type of question you get.
Which UITableViewCell to use? :
The preferred way would be to have different UITableViewCell
subclasses for each question you're getting.
Just having only one ultradynamic cell might get you in trouble with the reuseIdentifier because it wouldn't be re-used properly as there would be different controls on the view.
Here's a question that has a similar problem to solve:
2 different types of custom UITableViewCells in UITableView (With this approach you can avoid the reuseIdentifier
issue that might come up
Different Target Problem (iPhone / iPad):
Your other big requirement is, that you're running on iPhone and iPad so inside your UITableViewCell
you should determine which device you're running on right now and do custom initialization.
Determine device (iPhone, iPod Touch) with iPhone SDK
Doing this would save you from creating another set of UITableViewCell
s solely for the iPad. You wanna have at least this much dynamics in your code.
Managing the different question types:
For the different question types I would suggest you to set up an typedef enum which is incredibly flexible to use inside your app (as it's type safe) and easy to update once your requirements change.
HTH
There are multiple approaches to this issue and it solely depends on you which one you're comfortable with. Yet, I would suggest you to subclass UITableViewCell for each type of question you get.
Which UITableViewCell to use? :
The preferred way would be to have different UITableViewCell subclasses for each question you're getting.
Just having only one ultradynamic cell might get you in trouble with the reuseIdentifier because it wouldn't be re-used properly as there would be different controls on the view.
Here's a question that has a similar problem to solve:
2 different types of custom UITableViewCells in UITableView (With this approach you can avoid the reuseIdentifier issue that might come up
Different Target Problem (iPhone / iPad):
Your other big requirement is, that you're running on iPhone and iPad so inside your UITableViewCell you should determine which device you're running on right now and do custom initialization.
Determine device (iPhone, iPod Touch) with iPhone SDK
Doing this would save you from creating another set of UITableViewCells solely for the iPad. You wanna have at least this much dynamics in your code.
Managing the different question types:
For the diff
精彩评论