我正在处理表格。包含多个单元格供用户选择选项的表格。每个单元格都有一个标签作为问题和一个带有选项的选择器 View 作为答案。在单击“下一步”按钮之前如何获取单元格的所有值?选择器 View 的数据源是一个列表。我想要获取的值是选择器 View 选项的索引。谢谢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell", forIndexPath: indexPath) as! AddFollowTableViewCell
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
return cell
}
func getData()->[Int]{
}
func getCellsData() -> [Int] {
var dataArray: [Int] = []
for section in 0 ..< self.myTable.numberOfSections {
for row in 0 ..< self.myTable.numberOfRowsInSection(section) {
let indexPath = NSIndexPath(forRow: row, inSection: section)
let cell = self.myTable.cellForRowAtIndexPath(indexPath) as! AddFollowTableViewCell
dataArray.append(cell.cellValue)
}
}
return dataArray
}
最佳答案
你问:
How can I get all values for cells before I hit the Next button?
在 MVC design 中,当您点击“下一步”按钮时,您不应该从单元格中获取值。相反,随着选择器值的变化,您的 Controller 应该更新模型以反射(reflect)用户从选择器中选择的内容。然后,当您检测到“下一步”按钮被点击时,您已经拥有了模型中的所有选择。
这听起来可能很像您所描述的,但问题是适当的 TableView 设计允许单元格出列/重用,并且您不应该返回 View 以检索用户输入的内容在一些可能不再存在于内存中的视觉控件上。
关于快速获取tableview单元格的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262334/