UITableView Add, delete, move operations

Source: Internet
Author: User

#pragmaMark-----The move operation of the table view-----//The first step in the move is also to open the edit state of the table view//2. Specify which rows can be moved-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) Indexpath {//can be moved by default    returnYES;}//3. What to do after the move is complete, how to complete the mobile- (void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) Destinationindexpath {//record the model data in the original location firstStudent *student =_dataarray[sourceindexpath.row];        [Student retain]; //Delete model data from the original location[_dataarray RemoveObjectAtIndex:sourceIndexPath.row]; //Add the recorded model data to the data array in a new location[_dataarray insertobject:student AtIndex:destinationIndexPath.row]; [Student release];}#pragmaMark-----Delete, add data-----//1. Make the table view that you want to delete and add operations in edit State- (void) Setediting: (BOOL) editing animated: (bool) Animated {//This method in the parent class is executed first. [Super setediting:editing animated:animated]; //Table View executes this method[Self.tableview setediting:editing animated:animated];}//2. Specify which rows in the diagram can be in the edit State-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {//if (indexpath.row% 2 = = 0) {//return YES;//} else {//return NO;//    }    //all default rows can be edited    returnYES;}//3, specify the editing style, whether to delete or add the delegate protocol inside-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath {//If this method is not overridden, the default is to delete the style//return uitableviewcelleditingstyledelete;    returnUitableviewcelleditingstyleinsert;}//whether delete or add, this method is the core method of operation, when click Delete, or add a button, what needs to be done, how to complete the deletion or add the operation, all within this method is specified. - (void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {[TableView beginupdates];//Table View start update ( writable and writable)        if(Editingstyle = =uitableviewcelleditingstyledelete) {        //remove cells from this location[TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; //Delete data from the data array that is bound to the cell[_dataarray RemoveObjectAtIndex:indexPath.row]; } Else if(Editingstyle = =Uitableviewcelleditingstyleinsert) {Student*student =_dataarray[indexpath.row]; //Building a location informationNsindexpath *index = [Nsindexpath indexpathforrow:0Insection:0];        [TableView Insertrowsatindexpaths:@[index] withrowanimation:uitableviewrowanimationtop];    [_dataarray insertobject:student AtIndex:index.row]; } [TableView endupdates];//Table View End update (same can be written without writing, if written[TableView Beginupdates],endupdates also to write]
) }

UITableView Add, delete, move operations

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.