2016-07-17 63 views
3

我试图使用下面的代码编程方式选择在我的tableview的所有行:选择所有的TableView行编程方式使用selectRowAtIndexPath

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 

    } 


} 
+0

这只是字符串数据在我的模型。我认为我可以通过使用一个解决方法来实现这一点,通过使用一个元组数组,即作业标题和一个布尔值来指示该特定作业是否被选中。我只是觉得必须有一个更优雅的方式来实现这一点。 – Tom

+0

**永远不要**自己调用包含“will”,“should”和“did”的委托方法。它们完全由框架调用。 (一个罕见的例外是KVO的'will/didChangeValueForKey:') – vadian

回答

10

当时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) 
    } 
} 
+0

你是对的,我的确有空白。我已经添加了我的prepareForSegue函数来说明。我不明白的是,在我的prepareForSegue函数中,我遍历了我的indexPaths,我似乎必须确保该单元格在被添加到我的数据模型之前已经出队。必须有一种更清晰的方法来添加所有选定的单元格,而不必手动滚动表格。 – Tom

+0

当'allJobsSelected'变为true时,从代码中不清楚,但在发生这种情况时,应该为表的每一行调用'selectRowAtIndexPath'。这将标记所选的行。没有必要滚动到该行或加载数据。 – vacawama

2

对于斯威夫特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) 
     } 
    } 
} 
相关问题