Reputation: 468
I have to create one dynamic form which contains question answer set and questions are different for every user. There are 5 questions out of this user have to answer at least 3 questions. In the set of 5 questions, the first user may get the set question which required 1 textfield 2 drops down and 2 radio buttons in the same way the second user may get 3 textfield 1 drop down and 1 radio button. What approach I should have to follow to achieve this?
I tried to create a table view. In the cell, I specified one label to render a question and one blank view which will be filled later on the basis where the question required text field or radio button or drop down. But with this case, I am not able to maintain which question I get answered because if I make textfield or checkbox's user interaction unable the didselectrowatindexpath method is not getting called even tried to mapping with delegate but this one is also not suitable for me because I have so many cases to manage as an answer field.
Upvotes: 1
Views: 2542
Reputation: 476
There might be different implementations. What I would do is:-
My Implementation will go something like this for QuestionModel class:-
Class Question {
enum Type {
case type1, type2 .....
func cellIdentifier() -> String {
switch self {
case type1:
return "type1"
//Handle all cases
}
}
}
var type: Type
}
Controller class will be something like this:-
Class Controller: UIViewController {
var questions = [Question]()
//Table view delegate method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let questionAtIndexPath = questions[indexPath.row]
let cell = tableView.dequeReusableCell(withIdentifier: questionAtIndexPath.type.cellIdentifier(), for: indexPath)
cell.configureWith(questionAtIndexPath)
return cell
}
}
Upvotes: 1
Reputation: 11
Well, the main task you have is to create a dynamic form where you have different objects depending on the required question. So, do this first:
Connect the required delegates or IBOutlets with the objects
Create closure in table cells and handle the data when user completed answering the question.
Upvotes: 0