2016-04-08 36 views
3
let frc = NSFetchedResultsController(
     fetchRequest: alertsFetchRequest, 
     managedObjectContext: self.moc, 
     sectionNameKeyPath: "formattedDateDue", 
     cacheName: nil) 

当我使用NSFetchedResultsController截断我的记录时,如何展开和折叠我的表视图上的部分?使用NSFetchedResultsController展开/折叠UITableView部分NSFetchedResultsController

我见过很多教程,解释扩展和折叠单元格本身,但不是使用抓取结果控制器生成的部分上的任何东西。

+0

@ uday.m你的编辑不是很好。请勿将“Swift:”前缀添加到问题标题中。 –

回答

5

首先,你需要一个数组来保持跟踪每个部分是否展开或折叠:

var sectionExpandedInfo : [Bool] = [] 

获取的成果控制器已经完成了它的初始performFetch后,填充这个数组true每个部分(假设你想用默认扩展部分):

sectionExpandedInfo = [] 
for _ in frc.sections! { 
    sectionExpandedInfo.append(true) 
} 

修改numberOfRowsInSection方法返回零,如果该部分坍塌:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if sectionExpandedInfo[section] { // expanded 
     let sectionInfo = self.frc.sections![section] 
     return sectionInfo.numberOfObjects 
    } else { // collapsed 
     return 0 
    } 
} 

要切换节是展开还是不行,我用一个按钮为viewForHeaderInSection,与段名作为标题:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    if (self.frc.sections!.count > 0) { 
     let sectionInfo = self.frc.sections![section] 
     let sectionHeaderButton = UIButton(type: .Custom) 
     sectionHeaderButton.backgroundColor = UIColor.redColor() 
     sectionHeaderButton.setTitle(sectionInfo.name, forState: .Normal) 
     sectionHeaderButton.addTarget(self, action: #selector(MasterViewController.toggleSection(_:)), forControlEvents: .TouchUpInside) 
     return sectionHeaderButton 
    } else { 
     return nil 
    } 
} 

,并在toggleSection方法我再使用标题以确定哪个标题按钮已经被挖掘,展开/折叠的相应部分:

func toggleSection(sender: UIButton) { 
    for (index, frcSection) in self.frc.sections!.enumerate() { 
     if sender.titleForState(.Normal) == frcSection.name { 
      sectionExpandedInfo[index] = !sectionExpandedInfo[index] 
      self.tableView.reloadSections(NSIndexSet(index: index), withRowAnimation: .Automatic) 
     } 
    } 
} 

如果您的FRC插入或删除的部分,你需要更新sectionExpandedInfo t数组o包括/删除多余部分:

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { 
    switch type { 
     case .Insert: 
      self.sectionExpandedInfo.insert(true, atIndex: sectionIndex) 
      self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
     case .Delete: 
      self.sectionExpandedInfo.removeAtIndex(sectionIndex) 
      self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
     default: 
      return 
    } 
} 

再次假设您希望部分在默认情况下展开。

+0

工作完美!谢谢,@pbasdf – renx

+0

伟大的解决方案。谢谢。也许是一种改进。而不是使用按钮标题来确定节添加sectionHeaderButton.tag =部分viewForHeaderInSection,然后,在toggleSection,您可以引用sender.tag获取节号。 – guido

+0

@guido好主意。避免这些部分的笨重列举。 – pbasdf

0

如果要更改结果集,则需要更改请求中的谓词并再次调用performFetch()。然后,您可以更新您的表格。但是这可能会导致性能问题。您可以考虑其他更复杂的技术来管理您的模型视图绑定,例如为每个展开的部分使用不同的抓取结果控制器。当用户展开一个部分时,创建一个新的提取结果控制器,仅提取该部分的对象并更新您的表格视图。当用户折叠该部分时,放弃提取结果控制器。但是,这可能使您的表视图数据源实现复杂化。

相关问题