gpt4 book ai didi

swift - sectionIndexTitles(对于 tableView : UITableView) method returns strange results

转载 作者:搜寻专家 更新时间:2023-11-01 05:34:09 28 4
gpt4 key购买 nike

在我的带有 tableView 的 viewController 中,我有使用 fetchedResultsController 从核心数据数据库中获取的对象列表。我按部分对所有获取的结果进行了分组。它工作正常,我已经实现了以下方法来启用出现在 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 在我的 Core Data 数据库中有英文的 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。

最佳答案

我遇到了同样的问题,对于俄语,它返回了我!而不是字符。所以我执行了这个解决方案:

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
}

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

关于swift - sectionIndexTitles(对于 tableView : UITableView) method returns strange results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551243/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com