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

标签 ios swift selector

我在为 collat​​ionStringSelector 调用方法部分时遇到一些问题。它找不到我已正确定义的选择器。

这是我的方法调用:

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
    }
}

关于collat​​ionStringSelector 的 iOS/Swift UILocalizedIndexedCollat​​ion 方法部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49033285/

相关文章:

ios - iPhone X 的 UIView 高度没有扩展

swift - 最近 Swift 更新中关于变异结构的变化?

ios - 快速实现导航菜单的标准方法是什么?

swift - 从 Parse 加载数据到 TableView

jquery - 在 jQuery 中获取祖父

objective-c - Objective C 返回类型开关

ios - UIView动画问题

ios - 不在每个单元格中使用带有 CollectionView 的 UITableView 中的可重用单元格

html - 使用 CSS 的样式表之外的选择器

ios - 以编程方式 Swift 向 navigationBar 添加约束