gpt4 book ai didi

ios - 如何在 Swift 4 中按列值对 UITableView 进行排序?

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:36 25 4
gpt4 key购买 nike

我有一个应用程序可以显示比特币的价格。我想在单击列标题时进行排序。

enter image description here

我正在尝试执行此操作,已响应点击,但没有任何反应,该列继续未排序。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com