OBJECTIVE-C: deleting, inserting, moving cells

Source: Internet
Author: User

The specific instances of deleting, inserting, and moving cells are as follows:

The code is as follows:

1 #import "ViewController.h"2 #defineNUM 203typedefenum4 {5 Deletecell,6 Addcell,7 Movecell,8 }cellstate;9 @interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate>Ten@property (Weak, nonatomic) iboutlet UITableView *TableView; One@property (strong,nonatomic) Nsmutablearray *Arraym; A@property (assign,nonatomic) cellstate state;//The state to be processed for the cell - @end -  the @implementationViewcontroller - //Delete Cells --(Ibaction) addcellclicked: (Uibarbuttonitem *) Sender - { +Self.state =Addcell; -Self.tableView.editing =!self.tableView.editing; + } A //Inserting cells at-(Ibaction) deletecellclicked: (Uibarbuttonitem *) Sender - { -Self.state =Deletecell; -Self.tableView.editing =!self.tableView.editing; - } - //Move Cells in-(Ibaction) movecellclicked: (Uibarbuttonitem *) Sender - { toSelf.state =Movecell; +Self.tableView.editing =!self.tableView.editing; - } the  *- (void) Viewdidload { $ [Super Viewdidload];Panax Notoginseng      -     //Initialize theSelf.arraym =[Nsmutablearray arraywithcapacity:num]; +      for(intI=0; i<num; i++) A     { theNSString *productname = [NSString stringWithFormat:@"product-%02d", i+1]; + [Self.arraym addobject:productname]; -     } $      $     //set up data sources and proxies -Self.tableView.dataSource =Self ; -Self.tableview.Delegate=Self ; the } - Wuyi #pragmaMethods of Mark-tableview the //how many row of each section is there? --(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section Wu { -     returnSelf.arrayM.count; About } $ //set the contents of each cell --(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath - { -     //1. According to Reuseidentifier, first go to the object pool to find the reused Cell object A     StaticNSString *reuseidentifier =@"Productcell"; +UITableViewCell *cell =[Self.tableview Dequeuereusablecellwithidentifier:reuseidentifier]; the     //2. If not found, create a Cell object yourself -     if(Cell = =Nil) $     { theCell =[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseidentifier]; the     } the     //3. Set the contents of a Cell object theCell.textLabel.text =Self.arraym[indexpath.row]; -     returncell; in } the  the #pragmaMethods of Mark-tableview About //return cell Edit type the-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath the { the     if(Self.state = =Deletecell) +     { -         returnUitableviewcelleditingstyledelete;//Delete the     }Bayi     Else if(Self.state = =Addcell) the     { the         returnUitableviewcelleditingstyleinsert;//Insert -     } -     Else the     { the         returnUitableviewcelleditingstylenone;//Moving the     } the } - //make edits to cells the-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath the { the     //Remove the current cell94UITableViewCell *cell =[Self.tableview Cellforrowatindexpath:indexpath]; the     if(Cell.editingstyle = = Uitableviewcelleditingstyledelete)//Delete Cells the     { the         //1. Delete the cell98 [Self.arraym RemoveObjectAtIndex:indexPath.row]; About         //2. Partial Refresh Table - [Self.tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationmiddle];101     }102     if(Cell.editingstyle = = Uitableviewcelleditingstyleinsert)//Inserting cells103     {104         //1. Create a product resource theNSString *product = [NSString stringWithFormat:@"product-%02d", Arc4random_uniform ( -)];106         107         //2. Insert the data into the current cell108 [Self.arraym insertobject:product atIndex:indexPath.row];109          the         //3. Overall Refresh Table111 [Self.tableview Reloaddata]; the     }113 } the //Whether you can move the-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) Indexpath the {117     if(self.state = = Deletecell | | self.state = =Addcell)118     {119         returnNO; -     }121     Else122     {123         returnYES;124     } the }126 //Move Cells127-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) Destinationindexpath - {129     //Modify the order of elements in an array the     //Remove the element to be moved in the array33sNSString *item =[Self.arraym ObjectAtIndex:sourceIndexPath.row]; the     133     //Delete an element from its original location134 [Self.arraym RemoveObjectAtIndex:sourceIndexPath.row];135     136     //Reinsert a new element in a new location137 [Self.arraym Insertobject:item atIndex:destinationIndexPath.row];138 }139 @end

OBJECTIVE-C: deleting, inserting, moving cells

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.