gpt4 book ai didi

ios - Swift - 自动排序 UITableView

转载 作者:行者123 更新时间:2023-11-28 23:24:18 24 4
gpt4 key购买 nike

我有一个 UITableView,它基本上是一个 To-Do-List。有没有一种方法可以“自动重新排序”列表,以便“选中”单元格 自动进入列表底部,从而使“未选中”项目保持在顶部?

我希望你明白为什么我的问题是.. 有什么不清楚的就问。我找不到与该主题相关的任何内容,因此非常感谢您的帮助! :)

这是我的UITableView:

    import UIKit

class WhishlistTableViewController: UITableViewController {

public var wishList = [Wish]()


override func viewDidLoad() {
super.viewDidLoad()

// disable prefill tableview with cells
let v = UIView()
v.backgroundColor = .clear
tableView.tableFooterView = v

// disable didSelectAt
self.tableView.allowsSelection = false

self.tableView.register(WhishCell.self, forCellReuseIdentifier: WhishCell.reuseID)
self.wishList.append(Wish(withWishName: "Test"))

// add top inset for tavleview
self.tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)


}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return wishList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath) as! WhishCell
let currentWish = self.wishList[indexPath.row]
cell.label.text = currentWish.wishName
cell.backgroundColor = .clear




return cell
}

}

class Wish: NSObject {
public var wishName : String?
init(withWishName name: String) {
super.init()
wishName = name
}
}

最佳答案

每次编辑数组中的任何项目时都需要执行此操作

wishList.partition(by: { $0.checked }) // it's mutating and ignore the return 

我假设您在Wish 模型中checked bool 属性

关于ios - Swift - 自动排序 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096543/

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