sectionIndexTitles(对于tableView:UITableView)方法返回奇怪的结果

问题描述 投票:2回答:1

在我的带有tableView的viewController中,我有使用fetchedResultsController从Core Data Database中获取的对象列表。我按部分对所有提取的结果进行了分组。它工作正常,我已经实现了以下方法来启用显示在TableView右侧的A-Z sectionIndexes:

 func sectionIndexTitles(for tableView: UITableView) -> [String]? {
     let indexTitles = self.fetchedResultsController.sectionIndexTitles

        return indexTitles

    }

    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
         return self.fetchedResultsController.section(forSectionIndexTitle: title, at: index)
    }

我也有这个fetchedResultsController的声明

var fetchedResultsController: NSFetchedResultsController<Object>!

 self.fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context!, sectionNameKeyPath: "project.group.name", cacheName: nil)

sectionIndexTitles工作正常,而我的核心数据数据库中有英文的group.name值,但当我有另一种语言的group.name值(或英文混合而非英文)时,sectionIndexTitles显示(返回)我的英文字母,但是非英文字母,它会返回问号或英文字母。

例如,它返回像这个["A", "B", "F", "G", "R", "S", "A", "E"]的数组,而不是像这个["A", "B", "F", "G", "R", "S", "Letter in another language", "Letter in another language"]返回数组。我改变了语言方案,它返回["A", "B", "F", "G", "R", "S", "?", "?"]。它用英文字母替换非英文字母或显示“?”,而不是用另一种语言显示值。

enter image description here

不确定它是不是一个bug?或者也许它不能用不同的语言显示字母。问题在哪里以及如何解决?

我正在使用Swift 3,Xcode 8.3.3。

swift uitableview core-data swift3 tableview
1个回答
0
投票

我遇到了同样的问题,因为俄语会让我回复!而不是字符。所以我执行了这个解决方案

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    if let sections = fetchedResultsController?.sections {
        var sectionTitles: [String] = []

        for section in sections {
            sectionTitles.append(String(describing: section.name.first!))
        }
        return sectionTitles
    }

return fetchedResultsController?.sectionIndexTitles
}

我认为这可以用于大多数情况

© www.soinside.com 2019 - 2024. All rights reserved.