getting data from each UITableView Cells Swift

后端 未结 3 1767
挽巷
挽巷 2020-12-03 09:23

I have searched the internet countless times and have not found a solution to my situation. Things that may be a solution are things I didn\'t understand and they were in Ob

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 09:55

    To collect data from your cells after the user has finished entering the data (score and level), you can use the UITableView delegate method tableView:didDeselectRowAtIndexPath[Deselect]

    The code in your instance will go in UITableViewController class and will look something like this.

    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCustomCellClass
        var levelArray:[String] = [] //Assuming you want to collect and store your cell data in an Array. You may use a Dictionary as well, whichever is more convenient.
        var scoreArray:[String] = []
        // levelArray.append(cell.levelLabel.text!)
        // scoreArray.append(cell.scoreLabel.text!)
        levelArray.insert(cell.levelLabel.text!, atIndex: indexPath.row)
        scoreArray.insert(cell.scoreLabel.text!, atIndex: indexPath.row)
    }
    

    Next, in you submit button's action(Selector) function use the 'levelArray' and 'scoreArray' to pass your collected data. Be sure to declare the array/dictionary variables(ex: levelArray) right under your UITableViewController class declaration, like so

    class GpaCalculator: UITableViewController {
        var levelArray:[String] = []
        var scoreArray:[String] = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        // ...
    }
    

    ..to be able to use it in other functions like your submit button's action(Selector) function.

提交回复
热议问题