[英]UITableView edit mode not showing delete button
I'm replicating the sample Food Tracker app from apple and have an issue where the items from my TableView in Edit mode don't show the left hand delete icon (the round red one). 我正在从Apple复制样本Food Tracker应用程序,并且遇到一个问题,即在“编辑”模式下我的TableView中的项目不显示左手删除图标(红色的圆形)。 If I swipe left I get a delete button on the right.
如果我向左滑动,则会在右侧显示一个删除按钮。 I've downloaded the Food Tracker code and it works.
我已经下载了Food Tracker代码,它可以正常工作。
The difference I can see is that the sample app has implemented a UITableViewController, whereas I have a UITableView within a standard UIViewController. 我可以看到的区别是示例应用程序已实现了UITableViewController,而我在标准UIViewController中具有UITableView。 I'm not sure why this doesn't work.
我不确定为什么这行不通。
There are some similar questions but they are older versions of Swift / Ios, so I'm not sure they are still relevant. 有一些类似的问题,但是它们是Swift / Ios的旧版本,因此我不确定它们是否仍然有用。
Here is the link to the sample app https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1 这是到示例应用程序的链接https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1
Here are the funcs I recently added that should make it work 这是我最近添加的可使其正常工作的功能
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
//delete the row from the dataSource
dataSource.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
else if editingStyle == .insert {
//create a new instance and insert the row
}
}
And I added this to the ViewDidLoad() to get the Edit button in the Nav bar 并将其添加到ViewDidLoad()以在导航栏中获得“编辑”按钮
navigationItem.leftBarButtonItem = editButtonItem
Here's what the Food Tracker example looks like; 这是Food Tracker示例的外观;
enter image description here 在此处输入图片说明
and mine is missing the left hand button. 我的缺少左手按钮。 The only difference I can see is that I'm using a TableView inside a ViewController.
我可以看到的唯一区别是,我在ViewController中使用了TableView。
Thanks 谢谢
Nick 缺口
I think I solved this. 我想我解决了这个问题。 Effectively there's a fair bit of 'magic' that a UITableViewController is doing.
实际上,UITableViewController正在做的工作有很多“魔术”。 To recreate this in a normal UIViewController with a TableView you have to override the SetEditing function and based on the title of the button put it in edit mode.
要在具有TableView的普通UIViewController中重新创建此控件,必须重写SetEditing函数,并基于按钮的标题将其置于编辑模式。
override func setEditing(_ editing: Bool, animated: Bool) {
let status = navigationItem.leftBarButtonItem?.title
if status == "Edit" {
tableView.isEditing = true
navigationItem.leftBarButtonItem?.title = "Done"
}
else {
tableView.isEditing = false
navigationItem.leftBarButtonItem?.title = "Edit"
}
}
No need to check the title of the barButtonItem as tapping it already toggles its state and passes the correct state in editing param. 无需检查barButtonItem的标题,因为点击它已经切换了它的状态并在编辑参数时传递了正确的状态。
Also, when overriding, you need to invoke super's implementation as absence would always show the delete indicator. 同样,在覆盖时,您需要调用super的实现,因为不存在将始终显示delete指示符。
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)
tableView.setEditing(editing, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.