2016-04-08 39 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()。然後,您可以更新您的表格。但是這可能會導致性能問題。您可以考慮其他更復雜的技術來管理您的模型視圖綁定,例如爲每個展開的部分使用不同的抓取結果控制器。當用戶展開一個部分時,創建一個新的提取結果控制器,僅提取該部分的對象並更新您的表格視圖。當用戶摺疊該部分時,放棄提取結果控制器。但是,這可能使您的表視圖數據源實現複雜化。

相關問題