Akshay Digrase
Akshay Digrase

Reputation: 468

How to create dynamic form in iOS?

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

Answers (2)

Keshav Raj
Keshav Raj

Reputation: 476

There might be different implementations. What I would do is:-

  • Distinguish the Question Types based on the UI(as per your case)
  • Add a questionType enum in QuestionModel class
  • Will create a different cell for each question type
  • Use that particular cell for each question type
  • Even in future any new Qustion Type has come, add one more case in enum and create one more cell

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

Sachi
Sachi

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:

  1. Create individual tableCells like one containing a label for the question and a textfield for the answer or checkbox's to answer the question. The label will be common for every tableCell.
  2. Connect the required delegates or IBOutlets with the objects

  3. Create closure in table cells and handle the data when user completed answering the question.

  4. Finally save the data collected by the closures call backs in TableViewcellforRowAtIndexpath and use them as per required.

Upvotes: 0

Related Questions