Janice Zhan
Janice Zhan

Reputation: 571

swift get all values of tableview cell

I am working on a form. A table with several cells for users to choose options. Each cell has a label as question and a picker view with options as answer. How can I get all values for cells before I hit the Next button? The datasource of picker view is a list. And the values i want to get are the indexes of picker view's options. Thanks 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
}

Upvotes: 1

Views: 2406

Answers (1)

Rob
Rob

Reputation: 437452

You ask:

How can I get all values for cells before I hit the Next button?

In a MVC design, you shouldn't be getting the values from the cells when you hit the "Next" button. Instead, as the picker values change, your controller should update the model to reflect what the user selected from the picker. Then, when you detect the "Next" button having been tapped, you already have all of the choices in your model.

This might sound much like what you're describing, but the issue is that a proper table view design allows for cells to be dequeued/reused, and you shouldn't be going back to the view to retrieve what the user entered on some visual control that may no longer be in memory.

Upvotes: 3

Related Questions