我遇到需要在项目中显示索引表的情况。所以我正在为节标题标题和索引创建一个动态数组。我已创建数组,但无法对此数组进行排序。我有一个这样的对象:
struct StorageData {
var id: String?
var version: Int?
var downloadDate: Date?
var legId: String?
var packageId: String?
}
我从服务器获取了这个数组。所以我刚创建了两个数组。
var tableViewSource = [Character : [StorageData]]()
var tableViewHeaders = [Character]()
并尝试通过以下语句填充这些数组:
func getTableData(words: [StorageData]) {
tableViewSource = createTableData(wordList: words).source
tableViewHeaders = createTableData(wordList: words).firstSymbols
}
func createTableData(wordList: [StorageData]) -> (firstSymbols: [Character], source: [Character : [StorageData]]) {
// Build Character Set
var firstSymbols = Set<Character>()
func getFirstSymbol(word: String) -> Character {
return word[word.startIndex]
}
wordList.forEach {_ = firstSymbols.insert(getFirstSymbol(word: $0.name!)) }
// Build tableSourse array
var tableViewSource = [Character : [StorageData]]()
let sortedSymbols = firstSymbols.sorted(by: {$0 < $1})
for symbol in sortedSymbols {
var words = [StoragePersonal]()
for word in wordList {
let data = word.name
if symbol == getFirstSymbol(word: data!) {
words.append(word)
}
}
tableViewSource[symbol] = words.sorted(by: {$0.name! < $1.name!})
}
return (sortedSymbols, tableViewSource)
}
但tableViewSource
中的数据未分类。
答案 0 :(得分:0)
我想你想把数据分成几个部分。
我对数据类型StoragePersonal
感到困惑,它是StorageData
的孩子,不是吗。
我在name
结构中看不到StorageData
属性。
我认为你应该改变:
func getTableData(words: [StorageData]) {
tableViewSource = createTableData(wordList: words).source
tableViewHeaders = createTableData(wordList: words).firstSymbols
}
为:
func getTableData(words: [StorageData]) {
let result = createTableData(wordList: words)
tableViewSource = result.source
tableViewHeaders = result.firstSymbols
}
我创建了一个新功能来为您分隔数据:
func createTableData(wordList: [StorageData]) -> (firstSymbols: [Character], source: [Character : [StorageData]])
{
var tableViewSource = [Character : [StorageData]]()
var firstSymbols = [Character]()
// first, sort wordList
let sortedWordList = wordList.sorted {
if let name1 = $0.name {
if let name2 = $1.name {
let compareResult = name1.localizedCaseInsensitiveCompare(name2)
if compareResult == ComparisonResult.orderedSame || compareResult == ComparisonResult.orderedAscending {
return true
}
}
}
return false
}
// second, separate words into section by the first character
var previousCharacter = Character("A")
var previousWords = [StorageData]()
var otherWords = [StorageData]()
for word in sortedWordList {
if let name = word.name {
let upperCaseName = name.uppercased()
if let firstCharacter = upperCaseName.characters.first {
if (firstCharacter < Character("A")) || (firstCharacter > Character("Z")) {
// other word (numbers, special characters, ...)
otherWords.append(word)
} else if firstCharacter == previousCharacter {
previousWords.append(word)
} else {
// save a section
if previousWords.count > 0 {
tableViewSource[previousCharacter] = previousWords
firstSymbols.append(previousCharacter)
}
// start new section
previousCharacter = firstCharacter
previousWords.removeAll()
previousWords.append(word)
}
}
}
}
// save the last normal section
if previousWords.count > 0 {
tableViewSource[previousCharacter] = previousWords
firstSymbols.append(previousCharacter)
}
// save the other section
tableViewSource["#"] = otherWords
firstSymbols.append("#")
return (firstSymbols, tableViewSource)
}