在iOS开发中,如果你想在用户轻触按钮时撤销UITableView
单元格的重新排序,你可以采取以下步骤:
UITableView
启用了单元格的重新排序功能。UITableView
启用了单元格的重新排序功能。undoReordering
方法关联起来。undoReordering
方法关联起来。以下是一个简单的示例,展示了如何在Swift中实现上述功能:
class MyTableViewController: UITableViewController {
var dataSource = ["Item 1", "Item 2", "Item 3", "Item 4"]
var originalOrder: [Int]?
override func viewDidLoad() {
super.viewDidLoad()
tableView.isEditing = true
originalOrder = dataSource.enumerated().map { $0.offset }
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = dataSource[indexPath.row]
return cell
}
func undoReordering() {
dataSource = originalOrder!.map { index in dataSource[index] }
tableView.reloadData()
}
@IBAction func undoButtonTapped(_ sender: UIButton) {
undoReordering()
}
}
通过上述步骤,你可以在用户轻触按钮时撤销UITableView
单元格的重新排序。
领取专属 10元无门槛券
手把手带您无忧上云