目标:获取带有动态原型单元格的UITableView
,其中显示标签和带有pickerview
的文本字段。标签和pickerview
的内容由数组提供。
自从我第一次发布这个问题以来,我已经走了很长的路。我现在拥有的:
1:模型
struct PersonalDetail {
var question: String
var answers: [String]
var answerType: String
init(questions: String, answArray: [String], answType: String){
self.question = questions
self.answers = answArray
self.answerType = answType
}}
var personalDetailArray = [
PersonalDetail(questions: "Country", answArray: ["USA","UK","Germany"], answType: "String"),
PersonalDetail(questions: "Age", answArray: ["10","20","30"], answType: "Int"),
PersonalDetail(questions: "Length", answArray: ["100","200","300"], answType: "Int"),
PersonalDetail(questions: "Weigth", answArray: ["1","2","3"], answType: "Int"),
PersonalDetail(questions: "Education", answArray: ["High School","University","Coder"], answType: "String"),
PersonalDetail(questions: "Number", answArray: ["1","2","3"], answType: "Int")]
2:我已准备好整个设置(tableView,tableViewCell,标识符设置,连接插座等)。标签已使用以下方式正确显示:
let cell = tableView.dequeueReusableCellWithIdentifier("PersonalDetailsQuestion") as! PersonalDetailsTableViewCell
let questionsExtra = personalDetailArray[indexPath.row]
cell.personalDetailQ?.text = questionsExtra.question
cell.configurePickerView() //This is not working yet!
3:我想要的是:我的函数(称为:configurePickerView)可以从模型中的personalDetailArray.AnswArray获取pickerview的内容,我认为应该如下所示:
cell.configurePickerView(answAarray: questionsExtra.answers)
4:我需要你的帮助:在TabelViewCell类中,我创建了一个带有工具栏的pickerView,它自己很好地工作,但是我试图动态填充pickerview,最好存储输入所以我可以将它发送到我的数据库。 我能做些什么才能让它发挥作用?这就是我到目前为止所做的:
class PersonalDetailsTableViewCell: UITableViewCell, UIPickerViewDelegate {
@IBOutlet var personDetailA : UITextField!
@IBOutlet weak var personalDetailQ: UILabel!
internal func configurePickerView(// HOW DO I DEFINE MY INPUT, THE ARRAY?) {
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.backgroundColor = UIColor.whiteColor()
pickerView.showsSelectionIndicator = true
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let nextButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "nextPicker")
toolBar.setItems([doneButton, spaceButton, nextButton], animated: false)
toolBar.userInteractionEnabled = true
personDetailA.inputView = pickerView
personDetailA.placeholder = "Not Set"
personDetailA.inputAccessoryView = toolBar
if let text = NSUserDefaults.standardUserDefaults().stringForKey("test") {
personDetailA.text = text
}}}
extension PersonalDetailsTableViewCell: UIPickerViewDataSource {
func numberOfComponentsInPickerView(colorPicker: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return // NEEDS PROPER INPUT
}}
extension PersonalDetailsTableViewCell {
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{return // NEEDS PROPER INPUT
}
func nextPicker() {
NSUserDefaults.standardUserDefaults().setValue(personDetailA.text, forKey: "test")
personDetailA.nextResponder() // HOW DO I GET TO THE NEXT PICKERVIEW?}
func donePicker() {
NSUserDefaults.standardUserDefaults().setValue(personDetailA.text, forKey: "test")
personDetailA.resignFirstResponder() // HOW DO I SAVE THE INPUT DYNAMICALLY?}
}