繁体   English   中英

用于collat​​ionStringSelector的iOS / Swift UILocalizedIndexedCollat​​ion方法部分不起作用

[英]iOS/Swift UILocalizedIndexedCollation method section for collationStringSelector doesn't work

我在调用collationStringSelector方法部分时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)
    }
} 

这是元素类型应实现的我的协议

@objc protocol CollationIndexable : class {
    @objc var collationString : String { get }
}

这是实现协议和选择器属性的具体元素类型

extension Contact : CollationIndexable {

    @objc var collationString : String {
        return lastName
    }
}

UPDATE!

好的,我已经解决了这个问题,Contact类必须继承表格

NSObject的

使用您的示例使本教程适用于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()
        }
    }

这是元素类型应实现的我的协议

@objc protocol CollationIndexable {
    @objc var collationString : String { get }
}

这是实现协议和选择器属性的具体元素类型

extension Contact : CollationIndexable {

    @objc var collationString : String {
        return lastName
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM