Ios Swift-自动订购UITableView
我有一个Ios Swift-自动订购UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableView,它基本上是一个待办事项列表。是否有一种方法可以“自动重新排序”列表,使“选中的”单元格自动位于列表的底部,使“未选中的”项目保持在顶部 我希望你能理解为什么我的问题是。。只要问问有没有什么不清楚的地方。我找不到与该主题相关的任何内容,因此我非常感谢您的帮助!:) 这是我的UITableView: import UIKit class WhishlistTableViewController: UITableViewController { pu
UITableView
,它基本上是一个待办事项列表。是否有一种方法可以“自动重新排序”列表,使“选中的”单元格自动位于列表的底部,使“未选中的”项目保持在顶部
我希望你能理解为什么我的问题是。。只要问问有没有什么不清楚的地方。我找不到与该主题相关的任何内容,因此我非常感谢您的帮助!:)
这是我的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
我假设您已检查了模型中的bool属性,请检查此代码,如果我失败,请告诉我,您需要触摸单元来检查任务
import UIKit
class Wish: NSObject {
public var wishName : String?
public var flag : Bool?
init(withWishName name: String, flagStatus: Bool) {
super.init()
flag = flagStatus
wishName = name
}
}
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", flagStatus: false))
// 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
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .Checkmark
}
wishList[indexPath.row].flag = true
sortData()
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
wishList[indexPath.row].flag = false
sortData()
}
func sortData()
{
wishList.sort { $0.flag!}
tableView.reloadData()
}
}
创建一个名为var和所选布尔变量的Wish类
创建Wish类型的项目列表(您已完成此步骤)
在表格视图选择上(didSelectRow函数):
3.1。从项目列表中获取所选愿望
3.2。将愿望设置为选中(或取消选中)
3.3。重新排列项目列表:
wishList.sorted(by: { !$0.selected })
3.4重新加载表视图数据。当数据发生更改时<将调用code>didSet
。Tableview将立即重新加载
var wishList:[Wish]=[]{
迪塞特{
wishList.sorted(按:{!$0.selected})
tableview.reloadData()
}
}
是的,您可以创建一个新数组,该数组有一个标志:true或false,您可以对新数组进行排序并重新加载数据,向我显示您的代码,我将在第二节帮助您:)刚刚编辑了我的假定相关,但从未将属性声明为可选(wishName
),该属性使用非可选值初始化。您能详细说明一下吗?我不太清楚你不为我工作是什么意思。抱歉,我应该添加我通过单击我的单元格内的ui按钮来“检查”我的项目,并且不使用didSelectRowAtIndexPath
。该按钮的具体功能是什么?它在哪里声明?按钮在我的自定义wishCell
中声明,用户可以“勾选”它。我实际上在我的单元格中使用ui按钮来“检查”它。所以我根本不使用didSelectRow
。你的解决方案不能转移到我的案例中吗?这只是一个建议:遵循iOS开发的原则,你不会遇到这种问题。我真的不明白为什么在我的单元格中设置按钮
违反iOS原则,但我对这一点非常陌生,所以很高兴能得到每一个帮助:)这会抛出这个错误:context closure type'(Wish,Wish)throws->Bool'需要2个参数,但在闭包体中使用了1个参数