Upgrading the delete confirmation button

2023-05-16

本文转载自andy heydon个人博客

ANDY HEYDON

Upgrading the delete confirmation button

by Andy

Standard delete confirmationiOS has a nice pattern for deleting a row in a table, either tap a minus symbol in a red circle or swipe your finger along the row and a delete button slides in from the right, tap the button to confirm the action or tap elsewhere to cancel. The only problem is that if you have a different look and style than the default then the delete button looks out of place. 

I encountered this recently and decided to see if I could replace the delete button with one more in keeping with the rest of the graphic style in my MealSchedule app.

Searching for examples of what other people might have done led to a couple of general approaches. The first was to subclass UITableViewCell to get the ability to override the willTransitionToState: method that is invoked as a cell moves through various editing states, and the second was to walk through the view hierarchy of the newly animated delete button and modify it. There’s nothing wrong with first suggestion because it utilizes a standard API, but the second involves testing against internal class names and assuming a particular view hierarchy, neither of which have any place in any app, regardless of whether you are submitting it to the AppStore or not. Working against an API or utilizing ordinarily hidden features is a bad smell and a strong indication that you are doing something wrong.

So the first step to having a nice delete confirmation button is to subclass UITableViewCelland override willTransitionToState:. The method is called with a bit mask of the new states. We are interested in the situation where the delete confirmation button is about to be displayed.

The default transition will create the shiny delete button and animate it into place. We don’t want to do that because we want to create our own button, but we will leave the other transitions to their default for the time being, so we are careful as to when we invoke super.

The next step is to create the new button and animate it into place. MealSchedule has a UIButton subclass named MPButton that encapsulates my alternative presentation, so let’s extend the transition to include that.

I use an instance variable to hold a reference to the delete button. I don’t strictly need to do this – I could use a tag and viewWithTag: instead, but the default implementation of the delete confirmation button uses tags within its hierarchy and so keeping a reference is safer than a tag by avoiding any unintended side effects from the base classes looking for views. The RBRectCenterPositionY is a simple function I use to center a CGRect within a particular height. I also use a category on UIScreen to return various numbers, such as the standard 10 pixels that are added as padding to a table’s content.

Note that when the cell transitions out of the delete confirmation state we want to make sure the delete button is moved out of the way, though if the cell is being deleted (as flagged by the _isDeleting variable) when just fade the button to invisible rather than move it to the right. This works as a smoother effect during the actual delete, which we will see in a minute, but for now you can tap the red minus to animate the delete button in, and tap it again to animate the button away.

The button is worthless unless we can actually perform a delete so we need to provide a method for the action we specified in the button creation.

When the delete button is tapped, it will invoke the delete: method that in turn invokes another local method named commitEdit:. As my button is a complete replacement for the standard button then we need to invoke the same transitions that would normally occur. In this particular situation, my table is always in edit mode so we will always be transitioning back to the UITableViewCellStateShowingEditControlMask state. 

The table’s datasource is the class that handles the delete so we need to invoke the standard tableView:commitEditingStyle:forRowAtIndexPath: method. The only slight problem here is that we need to know our current index path, and that is not available to us from the perspective of our UITableViewCell world. Unfortunately neither is the knowledge of the containing table. The table is actually a piece of private information in the base UITableViewCell class, but is not exposed to subclasses. Therefore we need to create a property, named table here, to pass in the table reference when we create an instance of our cell. Note that you cannot call this property tableView because that will conflict with the private reference!

The final step is to handle the cancelation of the delete by tapping elsewhere in the table. The easiest way of doing that is to set up a UITapGestureRecognizer on the table.

The gesture recognizer is created when we transition into the delete confirmation state, and removed when we have transition out of the confirmation.

One small wrinkle that I discovered here was a difference between iOS5 and iOS6. In the later SDK, the tap on the delete button will override that of the tap gesture, meaning the delete: method is invoked directly, but on iOS5 (and possibly earlier but I am not targeting those platforms so I didn’t test them), the tap gesture fires and the button’s action does not. Hence the deleteConfirmation: method tests to see if the tap was actually over the delete button or not.

Custom delete confirmationNow I have a nice delete confirmation button that matches all the other buttons in the app, and has been implemented without the knowledge or use of internal classes or views. In addition to the code above, I added a couple of custom methods on my table delegate to dim the other visible controls to avoid any confusion over the delete action, but that is just a little detail in my particular implementation. 

I also decided to leave the red minus as is because it doesn’t look as out of place as much as the confirmation button. The only functionality I have not replicated from the default delete confirmation process is animating the red minus back to horizontal if the delete is cancelled. To be able to perform that animation with the standard button would require referencing the internal representation. Alternatively I could replace the button, which I might do if the lack of animation starts to bug me.

About these ads
frameborder="0" scrolling="no" src="http://rma-api.gravity.com/v1/api/intelligence/w2?sg=361837bc83c4c2cba7b350dff56a564f&pl=16&ug=&b=59&ad=&sp=332&sourceUrl=http%3A%2F%2Fandyheydon.com%2F2013%2F01%2F18%2Fupgrading-the-delete-confirmation-button%2F&frameUrl=http%3A%2F%2Fandyheydon.com%2F2013%2F01%2F18%2Fupgrading-the-delete-confirmation-button%2F&clientTime=1420472374104&ci=grv-personalization-16&pageViewId%5BwidgetLoaderWindowUrl%5D=http%3A%2F%2Fandyheydon.com%2F2013%2F01%2F18%2Fupgrading-the-delete-confirmation-button%2F&pageViewId%5BtimeMillis%5D=1420472374104&pageViewId%5Brand%5D=1770193197298795" style="margin: 0px; padding: 0px; border-width: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; overflow: hidden; width: 300px; height: 250px; opacity: 1;">

Share this:

class="post-likes-widget jetpack-likes-widget" name="like-post-frame-42280877-201-54aab0299e2c5" height="55px" width="100%" frameborder="0" src="http://widgets.wp.com/likes/#blog_id=42280877&post_id=201&origin=andyheydon.wordpress.com&obj_id=42280877-201-54aab0299e2c5" style="margin: 0px; padding: 0px; border-width: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; display: block; width: 700px; float: none; position: absolute; top: 0px;">

Related

iOS6 and Rotation Whack-a-MoleIn "iOS"

Infinitely scrolling tablesIn "iOS"

App Tour : Part 3 - Handling full screen videoIn "iOS"

PUBLISHED:  January 18, 2013
FILED UNDER:  iOS
TAGS:  delete confirmation :  UITableViewCell

3 Comments to “Upgrading the delete confirmation button”

  1. Zergon
    July 21, 2013 at 6:55 pm

    I’m from space. And I’m just passing by to say Thank You! Great job with this article.

    REPLY
  2. Fleta
    July 6, 2014 at 5:49 am

    I see a lot of interesting articles on your blog.
    You have to spend a lot of time writing, i know how to save you a lot of time, there is a tool that
    creates unique, SEO friendly articles in couple of
    seconds, just search in google – laranita’s free content source

    REPLY
  3. Charolette
    August 29, 2014 at 11:29 pm

    I read a lot of interesting articles here. Probably you
    spend a lot of time writing, i know how to save you a lot of time, there is an online tool that creates high quality, SEO friendly
    articles in minutes, just type in google – laranitas free
    content source

    REPLY

Leave a Reply 

Fill in your details below or click an icon to log in:

 

 

« Previous Post
Next Post »

RSS Feed RSS - Posts

 

Create a free website or blog at WordPress.com. The Manifest Theme.

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

Build a website with WordPress.com
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Upgrading the delete confirmation button 的相关文章

  • Rails 中的订单确认页面

    我一直在尝试为我的 Rails 应用程序创建一个订单确认页面 但不太确定如何以一种轻松的方式完成它 上面有几个答案this https stackoverflow com questions 445293 ruby on rails con
  • 跨越父按钮高度的 100%

    我有以下标记
  • android 中 EditText 内的可点击按钮(或任何视图)

    我想在我的 EditText 中有一个按钮或可单击的视图 以便我可以在单击它时执行一些操作 我能够在我的 EditText 中放置一个可绘制对象 这要归功于马科斯贝里戈 https stackoverflow com users 18191
  • 如何在单击按钮时在按钮上制作圆形波纹?

    背景 在 Android 的拨号器应用程序上 当您开始搜索某些内容时 单击 EditText 左侧的箭头按钮 您会在其上看到圆形波纹效果 问题 我也尝试过 但我得到的是一个矩形的
  • jQuery 上一个按钮在单击时返回到 Q。) 1 而不是一一向后退

    jQuery 上一个按钮无法按预期工作 基本上最好的解释方法是 如果我在回答问题 5 时单击上一个按钮 则它默认为问题 1 而不是问题 4 所以它默认为问题1 这是一个问题 该怎么办 jQuery 位于脚本标签的底部 if i Questi
  • 结合 EditText 和 Button?

    在某些应用程序中 我看到 EditText 小部件与右侧的按钮结合在一起 例如Twitter 应用程序中的搜索字段 http 2 bp blogspot com GTM W5mVPTU S rmYdiUTCI AAAAAAAAAEE hIO
  • Android - 在 AsyncTask 中执行后

    我目前有一个asyncTask在预执行时启动一个加载栏 在后台向服务器发送一些内容 在执行后关闭对话框并启用一个按钮 但是 由于 doInBackground 返回 null 我的后执行未执行 我试图弄清楚我能做些什么来让 postExec
  • 长文本对话框按钮不换行/挤出 - Android 5.0 棒棒糖上的材质主题

    在优化棒棒糖材质主题应用程序时 我遇到了这个恼人的问题 每当对话框按钮上有长文本时 如果文本不适合按钮栏的总宽度 则这些按钮的文本不会像以前的主题那样换行为多行 相反 以下按钮被挤出对话框 无法访问 见下图 Screenshot 到目前为止
  • 在屏幕上滑动手指激活按钮,就像按下按钮一样

    如果我的标题不够清楚 我会详细解释一下 假设我们有一个充满多个按钮 10 多个 的屏幕 我们按下一个按钮 激活 onTouch onClick 如果我们现在移动手指而不抬起它 我希望它激活它滑过的任何其他按钮 在这种特殊情况下 我希望当您滑
  • 从 C# Windows 窗体在 MS Word 中打开 MS Word 文档

    我希望能够通过单击表单上的按钮 从 C 表单中打开 MS Word 中已制作的 Word 文档 但不知道如何操作 请帮忙 Thanks 上次我使用 Excel 时 我使用以下代码打开它 Process Start FileLocation
  • 错误:SPAN_EXCLUSIVE_EXCLUSIVE 跨度的长度不能为零

    我的 Android 应用程序出现问题 我有一个按钮和一个关联的事件 但是当我第一次单击时出现错误 跨度不能有零长度 但是当我第二次单击时 事件 onclick 运行良好 看看我的java代码 public class MainActivi
  • Zurb Foundation:如何在调整大小到较小的屏幕时使按钮变小?

    在 Zurb Foundation 4 中 是否有一种方法可以在浏览器尺寸调整得较小或在较小的屏幕上时自动切换到较小的按钮样式 例如 当屏幕是标准桌面屏幕时 请执行以下操作 a href class primary button Butto
  • 如何让一个不可见的透明按钮起作用?

    查看 Unity 论坛和问答网站中的一些答案 如何制作隐形按钮的答案不起作用 因为删除与按钮关联的图像会使其不起作用 如何解决这个问题并保持不可见属性 同时允许按钮实际工作 这是 Unity 的怪异之处之一 100 的现实世界项目都需要这个
  • 如何设置按钮的大小?

    我将按钮放在带有 GridLayout 的 JPane 中 然后我用 BoxLayout Y AXIS 将 JPanel 放入另一个 JPanel 中 我希望 GridLayout 中的按钮是方形的 我使用 tmp setSize 30 3
  • 标题中的全日历自定义按钮

    我需要在同一页面上的两个 或更多 完整日历之间切换 并且希望将此功能添加到日历标题内的自定义按钮中 我在自定义按钮上发现了一些有趣的代码 但它有点过时 因为它引用的是 Fullcalendar v 1 6 1 而我正在使用 2 3 1 这是
  • Swift 3 中的隐藏按钮

    我刚刚开始编码 我真的很想知道如何隐藏按钮 我想做的是当我按下按钮时 Start 我想让开始按钮和后退按钮消失 这是通过插座和操作完成的 我已经搜索了一下 但是当我想做的时候 它说do关键字应该指定一个语句块 我希望有人能帮助我 连接插座后
  • 更改 WinForms 按钮突出显示颜色

    I found 这一页 https stackoverflow com questions 9260303 how to change menu hover color winforms 其中概述了如何更改 MenuStrip 及其项目的呈
  • 添加 Javascript 按钮来更改 iframe 的内容

    我正在尝试创建此页面 其中有一个 Iframe 并且我想添加一个按钮来显示 iframe 中的下一页 以及一个按钮来显示 iframe 中的上一页 我总共有 4 个页面要在名为 1 html 2 html 3 html 4 html 的 i
  • 如何使用 winforms 在 vb.net 中制作大型按钮网格(24x20 或类似)?

    我正在 vb net WinForms 中制作一个座位预订系统 我需要用户能够选择他们想要使用的座位并改变颜色 这样他们就可以知道它已选择 我开始尝试使用按钮 但 480 个按钮严重减慢了表单的加载时间 然后我尝试了在行 列中带有按钮的数据
  • 如何通过 Android 中小部件上的按钮运行活动?

    我正在开发一个由按钮组成的切换小部件 当按下时 我希望它运行一个活动而不打开任何东西 只是像往常一样在桌面上说 有没有办法通过桌面小部件上的按钮直接运行活动 谢谢 更新 现在我尝试在代码中切换静默模式而不运行新的活动 这是我当前的代码 当我

随机推荐