ios-Table View-demo7-cell editing

Source: Internet
Author: User

////ROOTTABLEVIEWCONTROLLER.M//Editcell////Created by Liyang on 14-4-29.//Copyright (c) 2014 Liyang. All rights reserved.//#import "RootTableViewController.h"@interfaceRoottableviewcontroller ()@end@implementationRoottableviewcontroller- (ID) Initwithstyle: (uitableviewstyle) style{ Self=[Super Initwithstyle:style]; if(self) {}returnSelf ;}- (void) viewdidload{[Super Viewdidload]; _fontarrary=[Nsmutablearray Arraywitharray:[uifont familynames]; Self.navigationItem.rightBarButtonItem=self.editbuttonitem;//This is to add a button to the navigation controller, and this button is actively called the following-(void) setediting: (BOOL) editing animated: (bool) Animated This method, This method and UITableView have a method of the same name, but it is not the same. Calling this function is what this editbuttonitem is for.}-(void) Setediting: (BOOL) editing animated: (BOOL) animated{//This is the method that inherits the parent Class View Controller    if(self.tableView.editing) {[Self.tableview setediting:no animated:yes]; }Else{[Self.tableview setediting:yes animated:yes]; }}//make our table view in edit or non-editable state-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{//This indicates which lines can be edited (add, delete, move)NSLog (@"Caneditrowatindexpath"); if(indexpath.row==0) {        returnNO; }    returnYES;}-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{//using proxy method to judge the editing style        if(indexpath.row==1) {NSLog (@"Uitableviewcelleditingstyleinsert"); returnUitableviewcelleditingstyleinsert; } NSLog (@"Uitableviewcelleditingstyledelete"); returnUitableviewcelleditingstyledelete;}//style of editingintCount=0;//Override to support editing the table view.- (void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{if(Editingstyle = =uitableviewcelleditingstyledelete) {[_fontarrary removeObjectAtIndex:indexPath.row];//Delete the data before you delete it[TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } Else if(Editingstyle = =Uitableviewcelleditingstyleinsert) {NSString*font_new=[nsstring stringWithFormat:@"newfont%d", Count]; [_fontarrary insertobject:font_new AtIndex:indexPath.row+1]; Nsindexpath*_aaindexpath=[nsindexpath indexpathforrow:indexpath.row+1InSection:indexPath.section];        [TableView Insertrowsatindexpaths:@[_aaindexpath] withrowanimation:uitableviewrowanimationright]; Count++; }}#pragmaMark-table View Data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[_fontarrary Count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier=@"Cell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(cell==Nil) {Cell=[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; UILabel*lable=[[uilabel Alloc]initwithframe:cgrectmake (0,0, the, -)]; Lable.backgroundcolor=[Uicolor Purplecolor]; Lable.tag=101;    [Cell.contentview addsubview:lable]; } UILabel*lable= (UILabel *) [Cell.contentview Viewwithtag:101]; Lable.text=_fontarrary[indexpath.row]; returncell;}- (void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Fromindexpath Toindexpath: (Nsindexpath *) toindexpath{NSString*text=[_fontarrary ObjectAtIndex:fromIndexPath.row];    [_fontarrary RemoveObjectAtIndex:fromIndexPath.row]; [_fontarrary Insertobject:text atIndex:toIndexPath.row];}//after the move is over, to modify the data//Override to support conditional rearranging of the table view.-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath{NSLog (@"Canmoverowatindexpath"); //Return NO If you don't want the item to be re-orderable.    returnYES;}//Summary: First cell display will determine whether the cell can be edited, and then edit the time to determine whether it can be edited, and then call to determine the cell's editing style, and then determine whether the cell can be moved, I see, for Mao superfluous, In the presence of the cell to determine whether or not to edit, I think, I think is out of the data structure, space for efficiency bar (purely speculative), the final completion of the submission of edits will also be called once, whether editable. @end

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.