使用selectRowAtIndexPath

时间:2016-07-17 13:21:23

标签: ios swift uitableview didselectrowatindexpath

我正在尝试使用以下代码以编程方式选择tableview中的所有行:

 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)

    }


}

我的问题是,当我转到下一个viewcontroller时,只有已经出列的行被添加到我的表的数据模型中。为了将所有行添加到我的表的数据模型中,我必须手动滚动整个表。如何更改此选项,以便将所有选定的行添加到我的表的数据模型中,而无需滚动整个表格?

我无法理解的是,在选择了所有行之后,我按照以下方式遍历我的indexPaths,但除非我首先滚动整个表,否则不会添加所有indexPath。

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

    }


}

3 个答案:

答案 0 :(得分:14)

allJobsSelected成立时,您需要为表格的每一行调用UITableView方法selectRowAtIndexPath(_:animated:scrollPosition:)。在我的例子中,我将此功能附加到我命名为Select All的右侧栏按钮项。从cellForRowAtIndexPath拨打电话肯定不是正确的地方。

@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)
    }
}

答案 1 :(得分:13)

对于 Swift 3 并按字面意思回答您的问题,无论您的代码如何。

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)
        }
    }
}

如果要通知tableview委托,请使用以下方法:

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)
        }
    }
}

答案 2 :(得分:1)

功能解决方案(Swift 5.1)

extension UITableViewDataSource where Self: UITableView {
  /**
   * Returns all IndexPath's in a table
   * ## Examples:
   * table.indexPaths.forEach {
   *    selectRow(at: $0, animated: true, scrollPosition: .none) // selects all cells
   * }
   */
  public var indexPaths: [IndexPath] {
     return (0..<self.numberOfSections).indices.map { (sectionIndex: Int) -> [IndexPath] in
        (0..<self.numberOfRows(inSection: sectionIndex)).indices.compactMap { (rowIndex: Int) -> IndexPath? in
           IndexPath(row: rowIndex, section: sectionIndex)
        }
        }.flatMap { $0 }
  }
}