我在为 collationStringSelector
调用方法部分时遇到一些问题。它找不到我已正确定义的选择器。
这是我的方法调用:
for element in elements {
if let indexable = element as? CollationIndexable {
let collationIndex = collation.section(for: indexable, collationStringSelector: "collationString")
if contentCollationIndexed[collationIndex] == nil {
contentCollationIndexed[collationIndex] = [Element]()
}
contentCollationIndexed[collationIndex]!.append(element)
}
}
这是我的元素类型应该实现的协议(protocol)
@objc protocol CollationIndexable : class {
@objc var collationString : String { get }
}
这是实现协议(protocol)和选择器属性的具体元素类型
extension Contact : CollationIndexable {
@objc var collationString : String {
return lastName
}
}
更新!
好的,我已经解决了问题,Contact 类必须继承 form
NSObject
最佳答案
用你的例子来制作this tutorial使用 Swift 4。
var objects: [CollationIndexable] = [] {
didSet {
sections = Array(repeating: [], count: collation.sectionTitles.count)
let selector = #selector(getter: CollationIndexable.collationString)
let sortedObjects = collation.sortedArray(from: objects, collationStringSelector: selector)
for object in sortedObjects {
let sectionNumber = collation.section(for: object, collationStringSelector: selector)
sections[sectionNumber].append(object as AnyObject)
}
self.tableView.reloadData()
}
}
这是我的元素类型应该实现的协议(protocol)
@objc protocol CollationIndexable {
@objc var collationString : String { get }
}
这是实现协议(protocol)和选择器属性的具体元素类型
extension Contact : CollationIndexable {
@objc var collationString : String {
return lastName
}
}
关于collationStringSelector 的 iOS/Swift UILocalizedIndexedCollation 方法部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49033285/