【发布时间】:2018-06-06 20:00:48
【问题描述】:
我正在尝试创建对数组进行排序并放入相应部分的表格视图。我遵循了这个教程:http://www.yudiz.com/creating-tableview-with-section-indexes/
我设法使第一个工作在 tableview 数组被排序的地方工作,即使没有数据的部分仍然出现。
第二个是关于解决没有数据的部分仍然出现这对我不起作用的问题。
在第二个之后,由于这个错误,我无法运行它
'[MyContact]' is not convertible to '[AnyObject]'
这是我的代码:
联系方式:
class MyContact: NSObject {
@objc var name: String!
@objc var mobile: String!
init(name: String, mob: String) {
self.name = name
self.mobile = mob
}
}
将数组划分为排序子类别的扩展
extension UILocalizedIndexedCollation {
func partitionObjects(array: [AnyObject], collationStringSelector: Selector) -> ([AnyObject], [String]) {
var unsortedSections = [[AnyObject]]()
for _ in self.sectionTitles {
unsortedSections.append([])
}
for item in array {
let index: Int = self.section(for: item, collationStringSelector: collationStringSelector)
unsortedSections[index].append(item)
}
var sectionTitles = [String]()
var sections = [AnyObject]()
for index in 0 ..< unsortedSections.count {
if unsortedSections[index].count > 0 {
sectionTitles.append(self.sectionTitles[index])
sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector) as AnyObject)
}
}
return (sections, sectionTitles)
}
}
数据源的元组和有错误的行
let (arrayContacts, arrayTitles) = collation.partitionObjects(array: self.myContacts, collationStringSelector: #selector(getter: MyContact.name)) as! [[MyContact]]
【问题讨论】:
-
将一行拆分成多个语句,你会看到错误。
-
看起来您正试图将元组强制转换为数组数组。这是合法的命令吗?
-
@GuyKogus 哦,天哪,我不敢相信我错过了,我可能是盲目输入的。无论如何发表您的评论作为答案,我会接受它。
标签: ios uitableview swift4 xcode9 swift3.2