ios - 从 fetchedResultsController 获取不需要的节数

标签 ios swift uitableview core-data nsfetchedresultscontroller

我在让 tableView 正确更新时遇到了问题,尽管我已经取得了一些进展。如果我的问题在于我的 fetchedResultsController 返回了不需要的部分,我认为问题的根源。

我的目标是让 SongSequences 显示在按 songSequenceTitle 排序的一个部分中。我对 fetchedResultsController 所做的工作最终将每个 SongSequence 视为一个新部分。

我有 Songs 作为 NSOrderedSet 添加到 SongSequence。这是我的 NSManagedObjects 的样子:

extension SongSequence {

    @NSManaged var songSequenceTitle: String!
    @NSManaged var songs: NSOrderedSet!

}

extension Song {

    @NSManaged var favorite: NSNumber!
    @NSManaged var songDescription: String!
    @NSManaged var songFilename: String!
    @NSManaged var songStar: String?
    @NSManaged var songSequences: NSOrderedSet!

}

当我将它们从 managedObjectContext 中拉出时,我的 fetchedResultsController 将每个 SongSequence 视为一个单独的部分,这对我造成了严重破坏当 managedObjectContext 发生变化时,NSFetchedResultsController 委托(delegate)方法和 tableView 更新方法。

lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest(entityName: "SongSequence")
    let sortDescriptor = NSSortDescriptor(key: "songSequenceTitle", ascending: true);
    fetchRequest.sortDescriptors = [sortDescriptor]

    let frc = NSFetchedResultsController (fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "songSequenceTitle", cacheName: nil)

    frc.delegate = self

    return frc
}()

我很困惑为什么 fetchedResultsController 将每个 SongSequence 视为一个单独的部分。

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    print("PlaylistVC numberOfSectionsInTableView = \(fetchedResultsController.sections!.count)")
    // This is equal to the number of SongSequences in my MOC
    return fetchedResultsController.sections!.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // This is always 1
    let numberOfRowsInSection = fetchedResultsController.sections?[section].numberOfObjects
    return numberOfRowsInSection!
}

如何在单个部分中显示所有 SongSequences

最佳答案

在您的抓取结果 Controller 中,您指定了一个 non-nil sectionNameKeyPath .这是按标题对歌曲序列进行分组,并且由于每个歌曲序列的标题都是不同的,因此每个歌曲都出现在自己的部分中。

A key path on result objects that returns the section name. Pass nil to indicate that the controller should generate a single section.

通过将 sectionNameKeyPath 设置为 nil 来初始化您的 frc,以避免按部分对歌曲序列进行分组。

let frc = NSFetchedResultsController (fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

关于ios - 从 fetchedResultsController 获取不需要的节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713193/

相关文章:

ios - 如何设置 UIScrollview 和动画的优先级

objective-c - 无法识别的选择器 : [NSSQLToMany _setInverseManyToMany:]

ios - tableViewCell 中的约束未正确显示

ios - Swift - 在后台推送 CollectionView

ios - UIDatePIcker TouchUp 事件以编程方式快速进行

iphone - 我可以在 iOS 上使用 AV Foundation Framework 流式传输 .mp3 链接吗?

ios - 从一个 View Controller 更改项目中所有 View 的背景颜色?

ios - 将 UIViewController 嵌入静态 UITableViewController 单元格中

iPhone UINavigationBar 向上/向下移动

swift - Swift 中的 Eureka 库 : How can I loop rows?