ios - 如何在 iOS 中创建动态表单?

标签 ios swift

我必须创建一个包含问题答案集的动态表单,并且每个用户的问题都不同。此用户有 5 个问题必须至少回答 3 个问题。在 5 个问题的集合中,第一个用户可能会得到需要 1 个文本字段 2 下拉菜单和 2 个单选按钮的设置问题,第二个用户可能会得到 3 个文本字段 1 下拉菜单和 1 个单选按钮。我应该遵循什么方法来实现这一目标?

我试图创建一个 TableView 。在单元格中,我指定了一个标签来呈现问题和一个空白 View ,稍后将根据问题需要的文本字段或单选按钮或下拉列表来填充。但是在这种情况下,我无法维护我得到回答的问题,因为如果我使文本字段或复选框的用户交互无法进行,即使尝试使用委托(delegate)进行映射,也不会调用 didselectrowatindexpath 方法,但这也不适合我,因为我有很多案例需要作为答案字段进行管理。

最佳答案

可能会有不同的实现。我会做的是:-

  • 根据用户界面区分问题类型(根据您的情况)
  • 在 QuestionModel 类中添加一个 questionType 枚举
  • 将为每个问题类型创建一个不同的单元格
  • 为每种问题类型使用特定的单元格
  • 即使将来出现任何新的问题类型,在枚举中再添加一个案例并再创建一个单元格

对于 QuestionModel 类,我的实现将是这样的:-

Class Question {
 enum Type {
  case type1, type2 .....
  func cellIdentifier() -> String {
   switch self {
    case type1:
         return "type1"
    //Handle all cases

   }
  }
 }

 var type: Type

}

Controller 类将是这样的:-

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
 }

}

关于ios - 如何在 iOS 中创建动态表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56184033/

相关文章:

ios - Sprite-Kit:移动 SKSpriteNode

ios - 有没有办法识别该应用程序是通过 didFinishLaunchingWithOptions 中的 firebase 动态链接安装的?

ios - 如何获得 Xcode 项目文件的较短路径

ios - 使用 Parse.com 未显示 Pins

ios - 如何在 TableView 搜索/过滤中获取项目的前一个 indexPath

arrays - 如何使用 Swift 3 将返回的数组转换为字典

由于换行导致 Swift 字符串比较失败

ios - swift : TableView cell size changing on run time

ios - 将文本字段留空 - 崩溃 - swift 4

ios - 在实际设备上使用 selenium webdriver 自动化 iOS 测试