快速获取tableview单元格的所有值

标签 swift tableviewcell

我正在处理表格。包含多个单元格供用户选择选项的表格。每个单元格都有一个标签作为问题和一个带有选项的选择器 View 作为答案。在单击“下一步”按钮之前如何获取单元格的所有值?选择器 View 的数据源是一个列表。我想要获取的值是选择器 View 选项的索引。谢谢 ScreenShot

     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/

相关文章:

ios - UITableViewCell 中的自动布局约束不匹配

ios - 以编程方式从带有动画的 customCell 类的 tableView 中删除一行

macos - 为什么我的 Split View会这样,我该如何解决?

iOS : Reduce image size without reducing image quality

ios - 预期返回 'UITableViewCell' 和两个 tableView 的函数中缺少返回值

ios - 在 SwiftUI TabView 中禁用滑动手势

ios - 在 UITableViewCell 中添加自定义编辑和删除按钮

ios - 我得到一个错误 - nib 必须恰好包含一个顶级对象,它必须是一个 UITableViewCell 实例'”

Swift reduce - 为什么 value 是可选的?

ios - 无法将 TableViewCell 类型的值转换为 'NSIndexPath'