gpt4 book ai didi

ios - 使用 selectRowAtIndexPath 以编程方式选择所有 TableView 行

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:23 24 4
gpt4 key购买 nike

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

}


}

我的问题是,当我转到下一个 View Controller 时,只有已经出队的行被添加到我的表的数据模型中。为了将所有行添加到我的表的数据模型中,我必须手动滚动整个表。我怎样才能改变这一点,以便将所有选定的行添加到我的表的数据模型中,而不必滚动整个表?

我无法理解的是,在选择了我的所有行之后,我会按如下方式循环遍历我的 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

}


}

最佳答案

对于 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 delegate,使用这个方法:

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

关于ios - 使用 selectRowAtIndexPath 以编程方式选择所有 TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38421681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com