我有一个应用程序可以显示比特币的价格。我想在单击列标题时进行排序。
我正在尝试执行此操作,已响应点击,但没有任何反应,该列继续未排序。
struct info {
let name: String
let coin: String
let change: Double
let price: Double
}
var ticker: Array<info> = []
@objc func tapChangeFunction(sender: UITapGestureRecognizer) {
self.ticker.sorted(by: {
$0.change > $1.change
})
DispatchQueue.main.async {
self.table.reloadData()
}
}
最佳答案
看来您刚刚忘记将排序后的数据分配回模型:
self.ticker = self.ticker.sorted(by: {
$0.change > $1.change
})
sorted(by:)
返回一个排序后的新数组,旧的没有修改,阅读docs :
Returns the elements of the collection, sorted using the given predicate as the comparison between elements.
或者使用变异版本 sort(by:)
( docs ):
Sorts the collection in place, using the given predicate as the comparison between elements.
self.ticker.sort(by: {
$0.change > $1.change
})
旁注:
您不必异步调度 reloadData
,您已经在主线程上了。
关于ios - 如何在 Swift 4 中按列值对 UITableView 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340628/