iosswiftuitableviewdidselectrowatindexpath

Select ALL TableView Rows Programmatically Using selectRowAtIndexPath


I'm trying to programmatically select all rows in my tableview using the following code:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:myTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! myTableViewCell

     cell.accessoryType = .None

    if allJobsSelected {

        let bgColorView = UIView()
        bgColorView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
        cell.contentView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
        cell.selectedBackgroundView = bgColorView
        cell.accessoryType = .Checkmark
        cell.highlighted = false

        cell.selected = true
        //  cell.accessoryType = .Checkmark
        self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
        self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)

    }

    var job: Jobs!

    job = jobs[UInt(indexPath.row)] as! Jobs

    cell.reports2JobTitle.text = job.jobTitle


    return cell
}

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.tableView.allowsMultipleSelection = true

    if let cell:myTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as? myTableViewCell {

        let bgColorView = UIView()
        bgColorView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
        cell.contentView.backgroundColor = UIColor(red: 250/255, green: 182/255, blue: 17/255, alpha: 1)
        cell.selectedBackgroundView = bgColorView
        cell.accessoryType = .Checkmark
        cell.highlighted = false
        self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.Bottom)

    }


}

My issue is that only the rows that have been dequeued are added to my table's data model when I segue to the next viewcontroller. In order to add all the rows to my table's data model I have to manually scroll through the whole table. How can I change this so all the selected rows are added to my table's data model without having to scroll through the whole table?

What I cannot understand is that after all my rows are selected I then loop through my indexPaths as follows but not all of the indexPaths are added unless I first scroll through the entire table.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if (segue.identifier == "reportsDisplay") {
        let controller = segue.destinationViewController as! ReportsDisplayViewController

        var selectedJob : Jobs!

        if let indexPaths = tableView.indexPathsForSelectedRows {


            for i in 0 ..< indexPaths.count {

                let thisPath = indexPaths[i]



                selectedJob = jobs[UInt(thisPath.row)] as! Jobs



                let jobTitle = selectedJob.jobTitle
                let id = selectedJob.identifier



                jobsToReport.append(jobTitle)
                jobsID.append(id)



            }


        }

        controller.reportedJobs = jobsToReport
        controller.idOfJobs = jobsID

    }


}

Solution

  • At the time allJobsSelected becomes true, you need to call the UITableView method selectRowAtIndexPath(_:animated:scrollPosition:) for each row of your table. In my case, I attached this functionality to the right bar button item which I named Select All. Calling this from cellForRowAtIndexPath is surely not the right place.

    @IBAction func doSelectAll(sender: UIBarButtonItem) {
        let totalRows = tableView.numberOfRowsInSection(0)
        for row in 0..<totalRows {
            tableView.selectRowAtIndexPath(NSIndexPath(forRow: row, inSection: 0), animated: false, scrollPosition: UITableViewScrollPosition.None)
        }
    }