使用selectRowAtIndexPath以编程方式选择所有TableView行 [英] Select ALL TableView Rows Programmatically Using selectRowAtIndexPath

查看:128
本文介绍了使用selectRowAtIndexPath以编程方式选择所有TableView行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

    }


}

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

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?

我不明白的是,在选择了所有行之后,我将按如下所示循环遍历indexPath,但是除非我先滚动整个表,否则不会添加所有indexPath.

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

    }


}

推荐答案

allJobsSelected 变为true时,您需要调用 UITableView 方法 selectRowAtIndexPath(_:animated:scrollPosition:)用于表格的每一行.就我而言,我将此功能附加到名为 Select All 的右侧栏按钮项上.从 cellForRowAtIndexPath 调用它肯定不是正确的地方.

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

这篇关于使用selectRowAtIndexPath以编程方式选择所有TableView行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆