Ios Swift:将UISegmentedControl/UITextField添加到动态UITableView

Ios Swift:将UISegmentedControl/UITextField添加到动态UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我正在创建一个注册表申请。该应用程序允许用户创建注册表,这些注册表使用UITableView显示。问题可以是文本输入(UITextField)或多项选择(UISegmentedControl)。我有创建问题,但我不知道如何在UITableView中显示UITextFields和UISegmentedControls。这是我用来创建问题的类: class Question { var Label: String var required: Int // create question init

我正在创建一个注册表申请。该应用程序允许用户创建注册表,这些注册表使用UITableView显示。问题可以是文本输入(UITextField)或多项选择(UISegmentedControl)。我有创建问题,但我不知道如何在UITableView中显示UITextFields和UISegmentedControls。这是我用来创建问题的类:

class Question {
var Label: String
var required: Int

// create question
init (Label: String, required: Int) {
    self.Label = Label
    self.required = required

    if (self.required == 0) {
        self.Label = self.Label + " *"
    }
}

}

class textInput: Question {
var placeHolder: String

init (placeHolder: String, Label: String, required: Int) {
    self.placeHolder = placeHolder
    super.init(Label: Label, required: required)
}
}

class multiChoice: Question {
var answers: [String]

init(answers: [String], Label: String, required: Int) {
    self.answers = answers
    super.init(Label: Label, required: required)
}
}
下面是我当前对CellForRowatineXpath的实现。显然,这将不得不改变以显示必要的内容

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    cell.textLabel?.text = self.questionsArray[indexPath.row].Label
    return cell
}
我考虑的一个可能的解决方案是生成UITextField和UISegmentedControl类变量,以便在类中轻松创建和初始化它们。但是,我仍然不知道如何实际显示它们。我怀疑是否有cell.textfield选项。非常感谢你的帮助

也许你应该试试:

  • 使用UITextField创建一个自定义UITableViewCell,使用UISegmentedControl创建另一个自定义UITableViewCell(不要忘记为每种单元格创建一个标识符)
  • 在单元格FORROWATINDEXPATH上,检查当前问题类型是textInput还是multiChoice,并使用适当的标识符显示正确的单元格

  • 我希望它有帮助

    我会试试看!很有道理!感谢您的帮助:欢迎光临。别忘了告诉我它是否有效,如果有效,就接受答案:)酷!听到这个我很高兴!