我创建了一个UITableView
,它从txt.file中获取数据。
但是,尽管txt-File按字母顺序排序,但UITableView不是。
有人知道如何按字母顺序排序/排序吗?
我正在使用Swift 3和Xcode 8.
var dictA = [String:String]()
var filtereddictA = [String:String]()
var visibleA = [ String ]()
var searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "a", ofType:"txt")
let filemgr = FileManager.default
if filemgr.fileExists(atPath: path!) {
do {
let fullText = try String(contentsOfFile: path! ,encoding: String.Encoding.utf8)
let readings = fullText.components(separatedBy: "\n")
for abteilungen in readings {
let aData = abteilungen.components(separatedBy: "\t")
if abteilungenData.count > 1 {
dictA[aData[0]] = aData[1]
}
}
filtereddictA = dictA
} catch let error {
print("Error: \(error)")
}
searchController = UISearchController (searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
}
self.title = "A"
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filtereddictA.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let key = Array(filtereddictA.keys)[indexPath.row]
cell.textLabel?.text = key
cell.detailTextLabel?.text = filtereddictA[key]
return cell
}
@available(iOS 8.0, *)
public func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text?.lowercased() else {
return
}
let keyPositions = Array(filtereddictA.keys)
filtereddictA = [:]
var removeArray = [ IndexPath ]()
var addArray = [ IndexPath ]()
for ab in dictA {
if abteilung.key.lowercased().range(of: searchText) != nil || a.value.lowercased().range(of: searchText) != nil || searchText.isEmpty {
filtereddictA[ab.key] = ab.value
} else {
if let index = keyPositions.index(of: abteilung.key) {
removeArray.append(IndexPath(row: index, section: 0))
}
}
}
if removeArray.count > 0 {
removeArray.sort(by: { (p1,p2) -> Bool in
return p1.row > p2.row
})
tableView.deleteRows(at: removeArray, with: .bottom)
}
var i = 0
for ab in filtereddictA.keys {
if !keyPositions.contains(ab) {
addArray.append(IndexPath(item: i, section: 0))
}
i += 1
}
if addArray.count > 0 {
addArray.sort(by: { (p1,p2) -> Bool in
return p1.row < p2.row
})
tableView.insertRows(at: addArray, with: .bottom)
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filtereddictA.count
}
答案 0 :(得分:3)
您应该在数据源中设置正确的元素顺序。
我可以假设filtereddictAbteilungen
是您是数据源的字典。
首先对其进行排序,然后通过tableView
进行填充。
示例:
let dictionary = [
"A" : [1, 2],
"Z" : [3, 4],
"D" : [5, 6]
]
let sortedKeys = dictionary.sorted(by: { $0.0 < $1.0 })