Upgrading the Delete Confirmation button

Source: Internet
Author: User
This article is reproduced from Andy Heydon personal blog
ANDY HeydonArchives about Upgrading the Delete Confirmation button by Andy

IOS has a nice to 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 the right, tap the button to confirm the action or tap elsewhere to cancel. The only problem is this if you have a different look and style than the default then the Delete button looks out of .

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

Searching for examples of the what other people might have do led to a couple the general approaches. The ability to override the willtransitiontostate:  Method This is invoked as a cell moves through various editing states, and the second being to walk through the view Hierarc Hy of the newly animated delete button and modify it. There ' s nothing wrong with the suggestion because it utilizes a standard API, but the second involves of testing in Ternal class names and assuming a particular view hierarchy, neither of which have any place in the any app, regardless of whe Ther you are submitting it to the App Store or not. Working against an API or utilizing ordinarily hidden features are a bad smell and a strong indication so you are doing s Omething wrong.

So the ' the ' the ' a nice delete confirmation button are to subclass Uitableviewcelland override Willtransitiontos Tate:. The method is called with a bit mask of the new States. We are interested in the situation where the delete confirmation button are about to be displayed.

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

The next step are to create the new button and animate it into place. Mealschedule has a UIButton subclass named Mpbutton that encapsulates I alternative presentation, so let ' s extend the TRA Nsition to include that.

I Use an instance variable to hold a reference to the Delete button. I don ' t strictly need to does 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 be safer than a tag by avoiding Any unintended side effects to the base classes looking for views. The rbrectcenterpositiony is a simple function I with to center A cgrect within a particular height. I also use a category on uiscreen to return various numbers, such as the standard pixels of that are added as PA dding to a table ' s content.

The ' when ' the cell transitions out of the ' delete confirmation ' we want to make sure the delete button is moved O UT of the way, though if the cell is being deleted (as flagged by the _isdeleting variable) when just fade the button to I Nvisible rather than move it to the right. This works as a smoother effect during the actual delete, which we'll be-to-a minute, but for now you can tap the red m Inus 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 deletes so we need to provide a of method for the action we specified In the button creation.

When the Delete button is tapped, it'll invoke The delete: method that in turn invokes another Named commitedit:. As my button are a complete replacement for the standard button then we need to invoke the same transitions this would norm Ally occur. In this particular situation, I table is always in edit mode so we'll always be transitioning back to The uitablev iewcellstateshowingeditcontrolmask state. 

The table ' s DataSource is the class this handles the delete so we need to invoke the standard Tableview:commiteditingstyle : Forrowatindexpath:method. The only slight problem this is "we need to know" our current index path, and "not" available to us from the Pers Pective of our own UITableViewCell world. Unfortunately neither is the knowledge of the containing table. The table is actually a piece's private information in the base UITableViewCell class, and the but is isn't exposed to subclasses. Therefore we need to create a property, named table here, to pass in the table reference when we create a instance of our Cell. This is the cannot call this property tableview because that would conflict with the private reference!

The final step was to handle the cancelation of the delete by tapping elsewhere in the table. The easiest way of doing of that are 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 Transiti On out of the confirmation.

One small wrinkle that I discovered this is a difference between iOS5 and iOS6. In the later SDK, the tap on the Delete button would override that's tap gesture, meaning the Delete:method is invoke D directly, but on iOS5 (and possibly earlier but I am is not targeting those "so I platforms ' t test didn)," tap them Re fires and the button ' s action does not. Hence the Deleteconfirmation:method tests to the If the tap is actually over the delete button or not.

Now I have a nice delete confirmation button that matches all the other buttons in the app, and has been implemented WI Thout the knowledge or use to 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 T O Avoid any confusion over the delete action, but this is just a little detail into my particular implementation. 

I also decided to leave the red minus as are because it doesn ' t look as out of the "as much as" the confirmation button. The only functionality I have not replicated to the default delete confirmation process is animating the red minus back To horizontal if the delete is cancelled. To is 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 to bug me. About these ads Share this: Twitter1 Facebook Related

IOS6 and rotation Whack-a-molein "IOS"

Infinitely scrolling tablesin "IOS"

APP tour:part 3-handling Full screen Videoin "IOS" Published: January, 2013 FILED, Under:ios Tags:dele Te Confirmation:uitableviewcell 3 Comments to "Upgrading the Delete Confirmation button" Zergon July 21, 2013 At 6:55 pm

I ' m from the space. And I ' m just passing by to say Thank you! Great job with this article. REPLY

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.