【问题标题】:sectionIndexTitles(for tableView: UITableView) method returns strange resultssectionIndexTitles(for tableView: UITableView) 方法返回奇怪的结果
【发布时间】:2018-01-15 00:22:27
【问题描述】:

在我的带有 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 工作正常,而我的核心数据数据库中有 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", "?", "?"]。 它用英文字母或显示“?”代替非英文字母,而不是用另一种语言显示值。

不确定这是否是错误?或者它可能无法显示不同语言的字母。问题可能出在哪里以及如何解决?

我使用的是 Swift 3,Xcode 8.3.3。

【问题讨论】:

    标签: swift uitableview core-data swift3 tableview


    【解决方案1】:

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

    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
    }
    

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

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多