简体   繁体   English

UITableView编辑模式不显示删除按钮

[英]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.

相关问题 在UITableview的编辑模式下滚动时删除按钮消失 - Delete button disappeared on scrolling in edit mode of UITableview 在编辑模式下按删除按钮时,UITableView崩溃 - UITableView crashes when pressing delete button in edit mode UITableView编辑模式删除按钮右侧填充 - UITableView edit mode delete button padding-right UITableView 编辑模式 - 自定义减号删除按钮的颜色 - UITableView edit mode - customize color of minus sign delete button UITableview编辑模式下的删除控制按钮不起作用 - Deletion control button on UITableview edit mode is not working 允许UITableView重新排序,但不能在编辑模式下删除,并启用滑动以删除 - Allow UITableView to reorder, but not delete in edit mode, and enable swipe to delete anyway UITableView禁用滑动以删除,但在编辑模式下仍具有删除功能? - UITableView disable swipe to delete, but still have delete in Edit mode? 在iOS 8.1中,当我在UITableView处于编辑模式时单击行的删除按钮时,该行及其上方的行显示不正确 - In iOS 8.1, when I click on a row's delete button while UITableView is in edit mode, the row and those above it display incorrectly 是否可以在UITableView编辑模式下更改默认删除操作图标? - Is it possible to change default delete action Icon in UITableView edit mode? 在编辑模式下删除表格行上的删除按钮⛔️ - remove the delete button ⛔️ on table rows in edit mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM