Cat learning iOS (14) UI UITableView Extension _ Table Modification _ (Add and remove Move)

Source: Internet
Author: User

Cat Share, must boutique
Material code Address: http://blog.csdn.net/u013357243/article/details/44727823
Original address: Http://blog.csdn.net/u013357243?viewmode=contents

Look first.

Code
//ps: New iOS Exchange Learning Group: 304570962 You can add a cat or a cat qq:1764541256 or Znycat Let's study hard together. Original: Http://blog.csdn.net/u013357243?viewmode=contents//HMVIEWCONTROLLER.M//06-Modification of the table////Created by Apple on 14-8-19.//Copyright (c) 2014 itcast. All rights reserved.//#import "HMViewController.h"  @interface hmviewcontroller () <Uitableviewdatasource, uitableviewdelegate >/** Data list * /@property(nonatomic,Strong)Nsmutablearray*datalist;@property(nonatomic,Strong)UITableView*tableview;@end @implementation hmviewcontroller - (UITableView*) tableview{if(_tableview = =Nil) {_tableview = [[UITableViewAlloc] initWithFrame: Self. View. BoundsStyle:uitableviewstyleplain]; _tableview. DataSource= Self; _tableview. Delegate= Self; [ Self. ViewAddsubview:_tableview]; }return_tableview;} - (Nsmutablearray*) datalist{if(_datalist = =Nil) {_datalist = [Nsmutablearrayarraywithobjects:@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 2nd", @"Cat No. 3rd", @"Cat No. 4th", @"Cat No. 5th",@"Cat No. 6th", @"Cat No. 7th", @"Cat No. 8th",@"Cat No. 9th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",@"Cat No. 1th", @"Cat No. 1th", @"Cat No. 1th",Nil]; }return_datalist;} - (void) viewdidload{[SuperViewdidload]; [ SelfTableView];//Start editing, once editing = = Yes to turn on Delete mode by default     Self. TableView. Editing=YES;} - (Nsinteger) TableView: (UITableView*) TableView numberofrowsinsection: (Nsinteger) section{return  Self. DataList. Count;} - (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) indexpath{Static NSString*id = @"Cell";UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:id];if(Cell = =Nil) {cell = [[UITableViewCellAlloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id]; }//Setup tableCell. Textlabel. Text= Self. DataList[Indexpath. Row];returnCell;}//As long as this method is implemented, it is possible to support gesture drag Delete, delete need to do it yourself! /** Uitableviewcelleditingstylenone, uitableviewcelleditingstyledelete, delete Uitableviewcelleditingstyleinsert Add * /- (void) TableView: (UITableView*) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath*) indexpath{if(Editingstyle = = Uitableviewcelleditingstyledelete) {NSLog(@"to delete");//MVC = data is saved in the model        //1. Delete indexpath data in Self.datalist[ Self. DataListRemoveobjectatindex:indexpath. Row];NSLog(@"%@", Self. DataList);//2. Refresh the table (reload data)        //Reload all data//[Self.tableview reloaddata];        //Deleterowsatindexpaths Let the table control animate delete the specified row[ Self. TableViewDeleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowanimationmiddle]; }Else if(Editingstyle = = Uitableviewcelleditingstyleinsert) {NSLog(@"To add Data");//1. Adding data to an array[ Self. DataListinsertobject:@"Wangxiao"Atindex:indexpath. Row+1];//2. Refresh the table//[Self.tableview reloaddata];        //Insertrowsatindexpaths Let the Table control animation add the specified row in the specified Indexpath        //Create a new Indexpath        Nsindexpath*path = [NsindexpathIndexpathforrow:indexpath. Row+1Insection:indexpath. section]; [ Self. TableViewInsertrowsatindexpaths:@[path] Withrowanimation:uitableviewrowanimationmiddle]; }}//As long as this method is implemented, you can display the drag control- (void) TableView: (UITableView*) TableView Moverowatindexpath: (Nsindexpath*) Sourceindexpath Toindexpath: (Nsindexpath*) destinationindexpath{//Interface Data UITableView has been completed.    //Adjustment data can be//[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. DataListRemoveobjectatindex:sourceindexpath. Row];NSLog(@"%@", Self. DataList);//3. Insert the source into the array at the target location[ Self. DataListInsertobject:source Atindex:destinationindexpath. Row];NSLog(@"%@", Self. DataList);}#pragma mark-Proxy method//Return to edit style, if not implemented, the default is delete-(Uitableviewcelleditingstyle) TableView: (UITableView*) TableView Editingstyleforrowatindexpath: (Nsindexpath*) indexpath{//if (indexpath.row% 2) {//return uitableviewcelleditingstyleinsert;//} else {//return uitableviewcelleditingstyledelete;//    }    returnUitableviewcelleditingstyleinsert;}@end
UITableView support for delete gestures

As long as the implementation of this method, you can support gesture drag Delete, delete need to do their own!

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

Uitableviewcelleditingstylenone, no effect.
Uitableviewcelleditingstyledelete, deleting
Uitableviewcelleditingstyleinsert add

To do in the delete:
Use [Self.tableview Reloaddata] when reloading data; inefficient

//MVC = data is saved in the model . Span class= "Hljs-comment" >//1. Delete the data in self.datalist indexpath  [self   removeobjectatindex:indexpath.row ]; nslog  (@ "%@" , self  );        //2. Refresh the table (reload data)  //reload all data         //[Self.tableview reloaddata];         //deleterowsatindexpaths let the table control animate delete the specified row  [self  .tableview  deleterowsatindexpaths:@[ Indexpath] withrowanimation:uitableviewrowanimationmiddle];  
UITableView Increase

To implement the Guard Agent method tableview.delegate = self;

// 返回编辑样式,如果没有实现此方法,默认都是删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//    if (indexPath.row % 2) {//        return UITableViewCellEditingStyleInsert;//    } else {//        return UITableViewCellEditingStyleDelete;//    }    return UITableViewCellEditingStyleInsert;}

Below is the-(void) TableView: (UITableView ) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath ) Indexpath method set in the
Determine the edit style, delete or add, then do the appropriate action

 if(Editingstyle = = Uitableviewcelleditingstyleinsert) {NSLog(@"To add Data");//1. Adding data to an array[ Self. DataListinsertobject:@"Mong Wang Wang Wang dog"Atindex:indexpath. Row+1];//2. Refresh the table//[Self.tableview reloaddata];        //Insertrowsatindexpaths Let the Table control animation add the specified row in the specified Indexpath        //Create a new Indexpath        Nsindexpath*path = [NsindexpathIndexpathforrow:indexpath. Row+1Insection:indexpath. section]; [ Self. TableViewInsertrowsatindexpaths:@[path] Withrowanimation:uitableviewrowanimationmiddle];
UITableView Mobile
//As long as this method is implemented, you can display the drag control- (void) TableView: (UITableView*) TableView Moverowatindexpath: (Nsindexpath*) Sourceindexpath Toindexpath: (Nsindexpath*) destinationindexpath{//Interface Data UITableView has been completed.    //Adjustment data can be//[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. DataListRemoveobjectatindex:sourceindexpath. Row];NSLog(@"%@", Self. DataList);//3. Insert the source into the array at the target location[ Self. DataListInsertobject:source Atindex:destinationindexpath. Row];NSLog(@"%@", Self. DataList);}

PS: New iOS Exchange Learning Group: 304570962 can add cat qq:1764541256 or Znycat Let's study hard together. Original: Http://blog.csdn.net/u013357243?viewmode=contents

Cat learning iOS (14) UI UITableView Extension _ Table Modification _ (Add and remove Move)

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.