iOS Seventh day (6:uitableview edit mode, drag position, swipe to delete)

Source: Internet
Author: User

UITableView edit mode, drag position, swipe to delete

#import "HMViewController.h"@interfaceHmviewcontroller () <uitableviewdatasource, uitableviewdelegate>/** Data List*/@property (nonatomic, strong) Nsmutablearray*dataList, @property (nonatomic, strong) UITableView*TableView;@end@implementationHmviewcontroller-(UITableView *) tableview{if(_tableview = =Nil) {_tableview=[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; _tableview.datasource=Self ; _tableview.Delegate=Self ;    [Self.view Addsubview:_tableview]; }    return_tableview;}-(Nsmutablearray *) datalist{if(_datalist = =Nil) {_datalist= [Nsmutablearray arraywithobjects:@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwu",@"Zhangsan",@"Lisi",@"Wangwuwangwuwangwuwangwuwangwu", nil]; }    return_datalist;}- (void) viewdidload{[Super Viewdidload];    [Self tableView]; //start editing, and once editing = = yes, the delete mode is turned on by defaultSelf.tableView.editing =YES;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.dataList.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *id =@"Cell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:id]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id]; }        //Setup tableCell.textLabel.text =Self.datalist[indexpath.row]; returncell;}//as long as the implementation of this method, you can support gesture drag Delete, delete need to do their own! /** Uitableviewcelleditingstylenone, Uitableviewcelleditingstyledelete, delete uitableviewcelleditingstyleinsert add */- (void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{if(Editingstyle = =uitableviewcelleditingstyledelete) {NSLog (@"to delete"); //MVC = data is stored in the model//1. Delete the Indexpath data in Self.datalist[Self.datalist RemoveObjectAtIndex:indexPath.row]; NSLog (@"%@", self.datalist); //2. Refresh the table (reload the data)//Reload All data//[Self.tableview Reloaddata]; //deleterowsatindexpaths to animate a table control to delete a specified row[Self.tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationmiddle]; } Else if(Editingstyle = =Uitableviewcelleditingstyleinsert) {NSLog (@"to add Data"); //1. Adding data to an array[Self.datalist InsertObject:@"Wangxiao"AtIndex:indexPath.row +1]; //2. Refresh the table//[Self.tableview Reloaddata]; //insertrowsatindexpaths The Table control animation to add the specified row in the specified Indexpath//Create a new IndexpathNsindexpath *path = [Nsindexpath IndexPathForRow:indexPath.row +1InSection:indexPath.section];    [Self.tableview Insertrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationmiddle]; }}//as long as you implement this method, you can display the drag control- (void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) destinationindexpath{//interface Data UITableView has been completed.//adjusting the data can//[Self.datalist ExchangeObjectAtIndex:sourceIndexPath.row WithObjectAtIndex:destinationIndexPath.row]; //1. Remove the source from the array    IDSource =Self.datalist[sourceindexpath.row]; //2. Remove the source from the array[Self.datalist RemoveObjectAtIndex:sourceIndexPath.row]; NSLog (@"%@", self.datalist); //3. Insert the source into the array at the target location[Self.datalist Insertobject:source AtIndex:destinationIndexPath.row]; NSLog (@"%@", self.datalist);}#pragmaMark-Proxy method//returns the edit style, which, if not implemented, is deleted by default-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{//if (indexpath.row% 2) {//return uitableviewcelleditingstyleinsert;//} else {//return uitableviewcelleditingstyledelete;//    }    returnUitableviewcelleditingstyleinsert;}@end

iOS Seventh day (6:uitableview edit mode, drag position, swipe to delete)

Related Article

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.