Select ALL TableView Rows Programmatically Using selectRowAtIndexPath

Tom picture Tom · Jul 17, 2016 · Viewed 12.3k times · Source

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

    }


}

Answer

Enrique picture Enrique · Aug 31, 2017

For Swift 3 and answering your question literally, regardless of your code.

func selectAllRows() {
    for section in 0..<tableView.numberOfSections {
        for row in 0..<tableView.numberOfRows(inSection: section) {
            tableView.selectRow(at: IndexPath(row: row, section: section), animated: false, scrollPosition: .none)
        }
    }
}

If you want to inform the tableview delegate, use this method:

func selectAllRows() {
    for section in 0..<tableView.numberOfSections {
        for row in 0..<tableView.numberOfRows(inSection: section) {
            let indexPath = IndexPath(row: row, section: section)
            _ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath)
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
            tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath)
        }
    }
}