IOS: Deletion of multiple cells (method I)

Source: Internet
Author: User

  Use Access Indexpath to delete multiple selected cells

Before deletion:

After deletion:

Analysis: How do I delete multiple cells? This requires the use of the UITableView proxy method, that is, the cell when the cell is selected, and we also define a mutable array to store the selected data for later deletion. This is the way to store Indexpath, because each cell is selected, it corresponds to a indexpath, and the selected cells are added to the Guide view, that is, check the tick, but also to determine whether the tick is repeated, based on this to display the cell tag. Finally, all of the tagged data is deleted in the original database, and the table is refreshed by emptying the array of stored data. The removal process involves sorting issues, which will be described in the following code.

1 #import "ViewController.h"2 #defineNUM 203 @interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate>4@property (strong,nonatomic) Nsmutablearray *products;//Original Product Inventory5@property (strong,nonatomic) Nsmutablearray *cellindexpaths;//hold the selected cells6@property (Weak, nonatomic) iboutlet UITableView *TableView;7-(Ibaction) deletebuttonclicked: (Uibarbuttonitem *) sender;8 9 @endTen  One @implementationViewcontroller A  -- (void) Viewdidload { - [Super Viewdidload]; the     //Initialize -Self.products =[Nsmutablearray arraywithcapacity:num]; -Self.cellindexpaths =[Nsmutablearray arraywithcapacity:num]; -      for(intI=0; i<num; i++) +     { -NSString *product = [NSString stringWithFormat:@"product-%02d", I]; + [Self.products addobject:product]; A     } at      -     //set up data sources and proxies -Self.tableView.dataSource =Self ; -Self.tableview.Delegate=Self ; - } -  in //Remove all Indexpath of selected cells - //Note: At each deletion, if you always delete from the back of the array, the result is not affected, but if you delete it from the front, then the elements in the array will move forward in order, and then their indexpath will change, and the result would be a problem. At this point, you need to sort the selected elements.  to-(Ibaction) deletebuttonclicked: (Uibarbuttonitem *) Sender + { -     //sort the selected elements (in ascending order) the[Self.cellindexpaths Sortusingcomparator:^nscomparisonresult (IDObj1,IDobj2) { *Nsindexpath *ip1 = (nsindexpath*) obj1; $Nsindexpath *ip2 = (nsindexpath*) Obj2;Panax Notoginseng         if(Ip1.row = =Ip2.row) -         { the             returnNsorderedsame; +         } A         Else if(Ip1.row >Ip2.row) the         { +             returnnsordereddescending; -         } $         Else $         { -             returnnsorderedascending; -         } the          -     }];Wuyi      the     //1. Remove the product from the original data in the selected cell (backward delete) -[Self.cellindexpaths Enumerateobjectswithoptions:nsenumerationreverse usingblock:^ (IDobj, Nsuinteger idx, BOOL *stop) { Wu[Self.products Removeobjectatindex: ((nsindexpath*) (obj). row]; -     }]; About      $     //2. Clear the selected array of records -Nsarray *temparray =[Nsarray arrayWithArray:self.cellIndexPaths]; - [Self.cellindexpaths removeallobjects]; -      A     //3. Perform a partial refresh + [Self.tableview Deleterowsatindexpaths:temparray withrowanimation:uitableviewrowanimationleft]; the } -  $ #pragmaData source methods for Mark-tableview the //how many row of each section is there? the-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section the { the     returnSelf.products.count; - } in //set the contents of each cell the-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath the { About     //1. According to Reuseidentifier, first go to the object pool to find the reused Cell object the     StaticNSString *reuseidentifier =@"Productcell"; theUITableViewCell *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];Bayi     } the     //3. Set the contents of a Cell object theCell.textLabel.text =Self.products[indexpath.row]; -      -     if([Self.cellindexpaths Containsobject:indexpath])//When you first select, Mark the     { theCell.accessorytype =Uitableviewcellaccessorycheckmark; the     } the     Else //when checked again, unmark -     { theCell.accessorytype =Uitableviewcellaccessorynone; the     } the     94     returncell; the } the  the #pragmaProxy methods for Mark-tableview98 //handling of selected cells About-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath - {101     //1. Remove the current cell102UITableViewCell *cell =[TableView Cellforrowatindexpath:indexpath];103     104     //3. Add the Indexpath of the removed cell to the array and add the Auxiliary guide view to the cell the     if([Self.cellindexpaths Containsobject:indexpath])//already exists106     {107Cell.accessorytype =Uitableviewcellaccessorynone;108         109         //Remove from array the [Self.cellindexpaths Removeobject:indexpath];111     } the     Else113     { theCell.accessorytype =Uitableviewcellaccessorycheckmark; the          the         //Add to Array117 [Self.cellindexpaths Addobject:indexpath];118     }119 } - 121 @end

IOS: Deletion of multiple cells (method I)

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.