【问题标题】:Select ALL TableView Rows Programmatically Using selectRowAtIndexPath使用 selectRowAtIndexPath 以编程方式选择所有 TableView 行
【发布时间】:2016-11-20 04:09:41
【问题描述】:

我正在尝试使用以下代码以编程方式选择我的 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)

    }


}

我的问题是,当我转到下一个视图控制器时,只有已出队的行才会添加到我的表的数据模型中。为了将所有行添加到我的表的数据模型中,我必须手动滚动整个表。如何更改此设置,以便将所有选定的行添加到我的表的数据模型中,而无需滚动整个表?

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

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

    }


}

【问题讨论】:

  • 我的模型中只有字符串数据。我想我可以通过使用一组元组的解决方法来实现这一点,即职位和一个布尔值来指示是否选择了该特定职位。我只是觉得必须有更优雅的方式来实现这一点。
  • 永远不会自己调用包含willshoulddid 的委托方法。它们由框架专门调用。 (一个罕见的例外是 KVO 的 will/didChangeValueForKey:

标签: ios swift uitableview didselectrowatindexpath


【解决方案1】:

对于 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】:

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

【讨论】:

  • 你说得对,我的知识确实有差距。我添加了 prepareForSegue 函数来说明。我不明白的是,在我的 prepareForSegue 函数中,我遍历了我的 indexPaths,我似乎必须确保在将单元格添加到我的数据模型之前,它已经出队。必须有一种更简洁的方法来添加我所有选定的单元格,而无需我手动滚动表格。
  • allJobsSelected 变为真时,您的代码并不清楚,但在发生这种情况时,您应该为表的每一行调用selectRowAtIndexPath。这将标记选定的行。无需滚动到该行或加载数据。
【解决方案3】:

功能解决方案(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 }
  }
}

【讨论】:

    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2011-11-12
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多