![](/img/trans.png)
[英]How to add multiple cells in header of a table view (not section header)?
[英]How to use NSFetchedResultsController to add a section (with a custom header cells) in a table view controller?
我正在制作一個具有可擴展表的表視圖,其中的節(束)是自定義UITableViewCell以及行(伙伴)-您可以創建任意一堆的伙伴。 我還使用NSFetchedResultsController填充了表,這已成功完成。 我遇到的困難是將一堆數據添加到核心數據時,NSFetchedResultsController拋出此異常:
CoreData:錯誤:嚴重的應用程序錯誤。 在調用-controllerDidChangeContent:期間,從NSFetchedResultsController的委托捕獲了一個異常。 嘗試將第0行插入第1節,但是在使用userInfo更新后,第1節中只有0行(空)
我希望能夠添加一堆(在模式對話框中),並使其自動顯示在表格視圖中(根據NSFetchedResultController功能),但是會引發異常(盡管不會崩潰),如上和本節所示未添加。
這是NSFetchedResultsController的代碼:
初始化 (在加載表視圖時初始化)
lazy var fetchedResultsController: NSFetchedResultsController = {
let bunchesFetchRequest = NSFetchRequest(entityName: Constants.CoreData.bunch)
// Sort bunches by bunch name, in alphabetical order, not caring about capitalization
let primarySortDescriptor = NSSortDescriptor(key: Constants.CoreData.Bunch.name, ascending: true, selector: "caseInsensitiveCompare:")
bunchesFetchRequest.sortDescriptors = [primarySortDescriptor]
// TODO: Answer question: Do we need this, does this benefit us at all, and how does prefetching work?
bunchesFetchRequest.relationshipKeyPathsForPrefetching = [Constants.CoreData.Bunch.buddies]
let frc = NSFetchedResultsController(
fetchRequest: bunchesFetchRequest,
managedObjectContext: CoreDataStackManager.sharedInstance().managedObjectContext!,
sectionNameKeyPath: Constants.CoreData.Bunch.name,
cacheName: nil
)
frc.delegate = self
return frc
}()
表格檢視方法
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// sections are the bunches
if let sections = fetchedResultsController.sections {
return sections.count
}
return 0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// sections are the bunches (this is for when sectionNameKeyPath is set)
if let sections = fetchedResultsController.sections {
let currentSection = sections[section] as! NSFetchedResultsSectionInfo
let bunch = currentSection.objects[0] as! Bunch
// Return the number of buddies in this section (bunch)
return bunch.buddies.count
}
return 0
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let sections = fetchedResultsController.sections {
let currentSection = sections[section] as! NSFetchedResultsSectionInfo
let bunch = currentSection.objects[0] as! Bunch
// Create BunchTableViewCell
let headerCell: BunchTableViewCell = tableView.dequeueReusableCellWithIdentifier(bunchCellIdentifier) as! BunchTableViewCell
headerCell.bunchNameLabel.text = bunch.name
return headerCell
}
return nil
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: BuddyInBunchTableViewCell = tableView.dequeueReusableCellWithIdentifier(buddyInBunchCellIdentifier, forIndexPath: indexPath) as! BuddyInBunchTableViewCell
if let sections = fetchedResultsController.sections {
let currentSection = sections[indexPath.section] as! NSFetchedResultsSectionInfo
let bunch = currentSection.objects[0] as! Bunch
let buddy: Buddy = bunch.getBuddiesInBunch()[indexPath.row]
cell.buddyFullNameLabel.text = buddy.getFullName()
}
return cell
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}
NSFetchedResultsController方法
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(
controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?) {
switch type {
case NSFetchedResultsChangeType.Insert:
// Note that for Insert, we insert a row at the __newIndexPath__
if let insertIndexPath = newIndexPath {
// AAAAAAAAAA
self.tableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
// BBBBBBBBBB
// self.tableView.insertSections(NSIndexSet(index: insertIndexPath.section), withRowAnimation: UITableViewRowAnimation.Fade)
}
case NSFetchedResultsChangeType.Delete:
// Note that for Delete, we delete the row at __indexPath__
if let deleteIndexPath = indexPath {
self.tableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
case NSFetchedResultsChangeType.Update:
// Note that for Update, we update the row at __indexPath__
// Not yet implemented
break
case NSFetchedResultsChangeType.Move:
// Note that for Move, we delete the row at __indexPath__
if let deleteIndexPath = indexPath {
self.tableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
// Note that for Move, we insert a row at the __newIndexPath__
if let insertIndexPath = newIndexPath {
self.tableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
}
func controller(
controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
// AAAAAAAAAA
let sectionIndexSet = NSIndexSet(index: sectionIndex)
self.tableView.insertSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
// BBBBBBBBBBB
// break
case .Delete:
let sectionIndexSet = NSIndexSet(index: sectionIndex)
self.tableView.deleteSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
注意:如果我注釋掉AAAAAAAAAA下的行並取消注釋BBBBBBBBBB中的行,則可以看到該單元格顯示的非常簡短,但是插入的單元格下面的每個單元格都消失了,而是多次出現此錯誤:
沒有重用表單元格的索引路徑
任何幫助/建議表示贊賞!
好吧,我發現:
“ NSFetchedResultsController的更大問題之一是它無法顯示空白部分。”
在此博客文章中提到了解決該問題的方法(注意:我尚未實現此方法,但我現在知道以直觀的方式是不可能的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.