uitaiteView不显示重新订购控件
当我使用trailwipeactionsconfigurationforrowat
时,我的tableview将显示删除和重新排序选项,但是选择重新排序
时,什么也不会发生。我认为我有所有正确的方法,并且正在调用设置
;我还有其他缺少的吗?谢谢!
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
func setupTableView() {
tableView.frame = self.view.frame
tableView.dataSource = self
tableView.delegate = self
tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")
tableView.dragInteractionEnabled = true
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.backgroundColor = .gray
cell.showsReorderControl = true
return cell
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.setEditing(editing, animated: animated)
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "delete") { (action, view, completion) in
tableView.reloadData()
completion(true)
}
let reorderAction = UIContextualAction(style: .normal, title: "reorder") { (action, view, completion) in
tableView.setEditing(true, animated: true)
completion(true)
}
return UISwipeActionsConfiguration(actions: [deleteAction, reorderAction])
}
}
class CustomCell: UITableViewCell {
}
滑动后的结果:
选择重新排序
:
< img src =“ https://i.sstatic.net/zgfowl.png” alt =“在此处输入图像说明”>
When I use trailingSwipeActionsConfigurationForRowAt
my TableView will show the delete and reorder options, however when selecting reorder
nothing happens. I think I have all of the correct methods and am calling setEditing
; is there anything else I'm missing? Thanks!
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
func setupTableView() {
tableView.frame = self.view.frame
tableView.dataSource = self
tableView.delegate = self
tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")
tableView.dragInteractionEnabled = true
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.backgroundColor = .gray
cell.showsReorderControl = true
return cell
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.setEditing(editing, animated: animated)
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: "delete") { (action, view, completion) in
tableView.reloadData()
completion(true)
}
let reorderAction = UIContextualAction(style: .normal, title: "reorder") { (action, view, completion) in
tableView.setEditing(true, animated: true)
completion(true)
}
return UISwipeActionsConfiguration(actions: [deleteAction, reorderAction])
}
}
class CustomCell: UITableViewCell {
}
Result after swiping:
After selecting reorder
:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些观察:
如果您不实现
tableView(_:moveRowAt:to:)
,您将不会获得重新排序控件,例如,假设您有一个模型,它是一个名为对象
,您可以执行以下操作:trailingSwipeActionsConfigurationForRowAt
可能不是放置“重新排序”命令的正确位置。部分原因是,一旦表格视图处于编辑模式并且您点击 ⛔️,就会显示尾随操作,并且“重新排序”在这种情况下没有意义。例如,在这里我点击 ⛔️,我看到了令人困惑的操作。我建议仅添加“删除”作为尾随操作。这样,您 (a) 在
isEditing
模式下点击 ⛔️ 时只能获得“删除”,而且 (b) 也可以获得独立的滑动操作。您无法从尾部滑动操作启动
isEditing
(并且,如上所述,无论如何,我认为您不想这样做)。因此,如果您在尾部滑动操作中没有“重新排序”,则需要其他方法来进入编辑模式。例如,在上面,我在导航栏中添加了一个“编辑”按钮,用于切换isEditing
:然后,您可以保持滑动删除功能,但是当您点击编辑按钮时,您可以点击 ⛔️ 删除功能(加上用于重新排序的句柄,因为我们添加了
tableView(_:moveRowAt:to :)
如上面第一步所述):实现重新排序的另一种方法是只允许在表格视图中进行拖放,您可以在其中长按一行然后拖动它:
通过设置
dragInteractionEnabled
和dropDelegate
启用此功能:显然,如果您要启用从此应用程序到其他应用程序的拖动功能,则需要在此处添加
UITableViewDragDelegate
一致性,并使您的模型对象符合NSItemProviderReading
和>NSItemProviderWriting
。但以上内容应该足以在UITableView
中进行拖放重新排序。A few observations:
You are not going to get the reorder controls if you do not implement
tableView(_:moveRowAt:to:)
, e.g., assuming you had a model which was an array calledobjects
, you could do the following:The
trailingSwipeActionsConfigurationForRowAt
is probably not the right place to put a “reorder” command. Part of the reason is that once the table view is in edit mode and you tap on the ⛔️, the trailing actions show up, and “reorder” does not make sense in that context. E.g., here I am tapping on ⛔️ and I see the confusing actions.I would suggest only adding “delete” as the trailing action. That way, you (a) get only “delete” if you tap on ⛔️ in
isEditing
mode, but also (b) get the stand-alone swipe action, too.You cannot initiate
isEditing
from the trailing swipe actions (and, as discussed above, I do not think you want to, anyway). So, if you do not have “reorder” in the trailing swipe actions, you need some other method to enter edit mode. E.g., above, I added an “edit” button to the navigation bar that togglesisEditing
:Then, you can keep the swipe to delete functionality, but when you tap on edit button, you have the tap on ⛔️ to delete functionality (plus the handles for reordering because we added
tableView(_:moveRowAt:to:)
as outlined in step one, above):Another way to achieve reordering is to just allow drag and drop within the table view where you can long-press on a row and then drag it:
This is enabled by setting
dragInteractionEnabled
anddropDelegate
:Clearly, if you were going to enable drag from this app to others, you would add
UITableViewDragDelegate
conformance here, and make your model objects conform toNSItemProviderReading
andNSItemProviderWriting
. But the above should be sufficient for dragging and dropping to reorder within aUITableView
.