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.
- #import <UIKit/UIKit.h>
- @interface Uitablecellswapdeleteviewcontroller:uiviewcontroller <uitableviewdelegate>{
- Iboutlet UITableView *testtableview;
- Nsmutablearray *dataarray;
- }
- @property (nonatomic, retain) UITableView *testtableview;
- @property (nonatomic, retain) Nsmutablearray *dataarray;
- @end
- -(void) viewdidload {
- [Super Viewdidload];
- DataArray = [[Nsmutablearray alloc] initwithobjects:@"1"@"2"@"3" ,@ "4" ,@ "5" , nil];
- }
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.
- -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
- //Return the number of sections.
- return 1;
- }
- -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
- //Return The number of the rows in the section.
- return [DataArray count];
- }
- //Customize The appearance of Table view cells.
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
- Static nsstring *cellidentifier = @ "Cell" ;
- UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
- if (cell = = nil) {
- cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];
- }
- //Configure the cell ...
- Cell.textLabel.text = [DataArray objectAtIndex:indexPath.row];
- return cell;
- }
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.
- -(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {
- return YES;
- }
- -(void) TableView: (UITableView *) TableView Commiteditingstyle: (Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {
- if (Editingstyle = = uitableviewcelleditingstyledelete) {
- [DataArray RemoveObjectAtIndex:indexPath.row];
- //Delete The row from the data source.
- [Testtableview Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationfade];
- }
- Else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {
- //Create A new instance of the appropriate class, insert it into the array, and add a new row to the table view.
- }
- }
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:
- -(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) indexpath{
- return @ "Download" ;
- }
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