IOS UITableView implementation of scrub removal

Source: Internet
Author: User
Tags uikit

Tags: swipe delete iphone slide Delete iOS UITableView original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://rainbird.blog.51cto.com/211214/634587

I knew nothing about Apple from July or August ago, and now I'm holding Iphone,ipad. Itouch has three online mature apps and is proficient in developing iOS apps. All the way up to the shoulders of the predecessors continue to progress. Now life and work stability is the time to sort out some of the things that have been done. Think about it and decide to talk about UITableView first.

For app applications, use the form of a list to show that the data is non-uitableview. After mastering the use of UITableView display data, are you also encountering the need to delete data? Do you think it's cool to have a swipe on a row of data and then a delete button? Talk less, go straight to the point, the author will show you how easy this feature is to implement. Previous preparations: The first step is to prepare the data source.
  1. #import <UIKit/UIKit.h>  
  2. @interface Uitablecellswapdeleteviewcontroller:uiviewcontroller <uitableviewdelegate>{
  3. Iboutlet UITableView *testtableview;
  4. Nsmutablearray *dataarray;
  5. }
  6. @property (nonatomic, retain) UITableView *testtableview;
  7. @property (nonatomic, retain) Nsmutablearray *dataarray;
  8. @end
  9. -(void) viewdidload {
  10. [Super Viewdidload];
  11. DataArray = [[Nsmutablearray alloc] initwithobjects:@"1"@"2"@"3" ,@ "4" ,@ "5" , nil];
  12. }
Here the author defines and implements a one-dimensional mutable array. Why use a mutable array? Because we want to delete the data inside. The second step is to show the data.
  1. -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
  2. //Return the number of sections.  
  3. return 1;
  4. }
  5. -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
  6. //Return The number of the rows in the section.  
  7. return [DataArray count];
  8. }
  9. //Customize The appearance of Table view cells.  
  10. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
  11. Static nsstring *cellidentifier = @ "Cell" ; 
  12. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
  13. if (cell = = nil) {
  14. cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];
  15. }
  16. //Configure the cell ...  
  17. Cell.textLabel.text = [DataArray objectAtIndex:indexPath.row];
  18. return cell;
  19. }
Data was added to UITableView by implementing the above three proxy methods.

Through the above two steps to achieve the data display work, then the implementation of key data deletion.
  1. -(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {
  2. return YES;
  3. }
  4. -(void) TableView: (UITableView *) TableView Commiteditingstyle: (Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {
  5. if (Editingstyle = = uitableviewcelleditingstyledelete) {
  6. [DataArray RemoveObjectAtIndex:indexPath.row];
  7. //Delete The row from the data source.  
  8. [Testtableview Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationfade];
  9. }
  10. Else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {
  11. //Create A new instance of the appropriate class, insert it into the array, and add a new row to the table view.  
  12. }
  13. }
Enable the above two agents and increase data deletion operations: [DataArray RemoveObjectAtIndex:indexPath.row]; Swipe to the right on a piece of data.

Click Delete.

Did you successfully delete a piece of data? The story is supposed to be done here. But I want to extend it. Notice the "Delete" after the two strokes, do you want to get rid of this thing impulse? For example: Download? In fact, the following proxy method:
    1. -(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) indexpath{
    2. return  @ "Download" ; 
    3. }
Again, is it a change?

Related articles:

UITableView Multi-Select Delete, similar to the multi-select Delete effect in Mail

Specific code See attachment

This article is from the "Rainbird" blog, make sure to keep this source http://rainbird.blog.51cto.com/211214/634587

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.