简体   繁体   English

从UITableViewCell隐藏删除按钮

[英]Hide Delete button from UITableViewCell

  • When my table view is in edit mode, the red (-) buttons appear. 当我的表格视图处于编辑模式时,出现红色(-)按钮。
  • When the user taps one of them the [Delete] button appears. 当用户点击其中之一时,将出现[删除]按钮。
  • When the user taps on [Delete] I first check a few things (partly online). 当用户点击[删除]时,我首先检查了几件事(部分在线)。 This delete may not be allowed. 可能不允许此删除。

  • When deleting that cell is not allowed, how do I hide the [Delete] button and let the red (|) button become a (-) again in an animated way? 当不允许删除该单元格时,如何隐藏[Delete]按钮并让红色(|)按钮以动画方式再次变为(-)? So, I don't want my whole table to leave editing state. 因此,我不希望我的整个表格离开编辑状态。

I've run into this issue myself where I may bring up an alert view to prompt the user further and wish to reset the delete button if they choose not to proceed. 我本人已经遇到了这个问题,在这里我可能会弹出一个警报视图来进一步提示用户,如果他们选择不继续操作,则希望重置删除按钮。 This seems like the easiest approach, assuming deleteIndexPath is the index path of the row selected for deletion: 假设deleteIndexPath是选择删除的行的索引路径,这似乎是最简单的方法:

[self.tableView reloadRowsAtIndexPaths:@[deleteIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

To get the actual animation (Instead of the UITableViewRowAnimationRight / UITableViewRowAnimationAutomatic ) animations, just do 要获取实际的动画(而不是UITableViewRowAnimationRight / UITableViewRowAnimationAutomatic )动画,只需执行

[self.tableView beginUpdates];
[self.tableView setEditing:NO animated:NO];
[self.tableView setEditing:YES animated:NO];
[self.tableView endUpdates];

beginUpdates and endUpdates provide the animation, and the tableView is just switched from not editing to editing instantly, which closes the delete button. beginUpdatesendUpdates提供了动画,并且tableView刚刚从不编辑切换为立即编辑,从而关闭了删除按钮。

Hope this helps! 希望这可以帮助!

I now see that you want to disable delete for only certain cells. 现在,我看到您只想对某些单元禁用删除。 You can do this in a couple of ways: 您可以通过以下两种方式执行此操作:

tableView:canEditRowAtIndexPath method: Return NO where you want DELETES to be DISABLED. tableView:canEditRowAtIndexPath方法:在要禁用DELETES的位置返回NO。 tableView:canMoveRowAtIndexPath : Return YES where you want to allow reordering. tableView:canMoveRowAtIndexPath :要允许重新排序的位置返回YES。

You may want to think about sub-classing UITableViewCell to give it some ability to maintain its own state (so the cell knows if delete is allowed or not. Then you can interrogate the actual cell instance and determine if you should enable delete even after the list may be re-ordered. 您可能需要考虑对UITableViewCell进行子类化,以使其具有保持其自身状态的能力(因此,单元格知道是否允许删除。然后,您可以询问实际的单元格实例,并确定即使在列表可能会重新排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM