Ios.uikit.17.uitableview--Cells operation

Source: Internet
Author: User

1. Case Description: Add, delete, move cells in table view, 01,02

Fig. 01 FIG. 02

2,. h

#import <UIKit/UIKit.h>@interface cq26viewcontroller:uiviewcontroller< uitableviewdatasource,uitableviewdelegate,uitextfielddelegate> ** * *  Listteams; @end

3,. m

#import "CQ26ViewController.h"@interfaceCq26viewcontroller ()@end@implementationCq26viewcontroller- (void) viewdidload{[Super Viewdidload]; //1. Set the navigation barSelf.navgationItem.rightBarButtonItem =Self.editbuttonitem; Self.navgationItem.title=@"cell insertion and deletion"; //2. Set the cell text boxSelf.txtField.hidden =YES; Self.txtfield.Delegate=Self ; //3. Assigning the current view controller to delegates and data sources that represent graphsSelf.tableview.Delegate=Self ; Self.tableView.dataSource=Self ; Self.listteams= [[Nsmutablearray alloc] Initwithobjects:@"Heilongjiang",@"Jilin",@"Liaoning", nil];}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return  -;}#pragmaMark-Uiviewcontroller life cycle method for responding to changes in view editing state-(void) Setediting: (BOOL) editing animated: (bool) animated {[Super setediting:editing animated:animated];    [Self.tableview setediting:editing Animated:yes]; if(editing) {Self.txtField.hidden=NO; } Else{Self.txtField.hidden=YES; }}#pragmaMark Uitableviewdatasource Protocol Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[Self.listteams Count] +1;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//1. Load cell    StaticNSString *cellidentifier =@"Cell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; }    //2. Set the contents of the cellBOOL B_addcell = (Indexpath.row = =self.listTeams.count); if(!B_addcell) {Cell.accessorytype=Uitableviewcellaccessorydisclosureindicator; Cell.textLabel.text=[Self.listteams ObjectAtIndex:indexPath.row]; }Else{self.txtField.frame= CGRectMake (Ten,0, -, -); Self.txtField.text=@"";    [Cell.contentview AddSubview:self.txtField]; }        returncell;}#pragmaMark Uitableviewdelegate Protocol Method-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{if(Indexpath.row = =[Self.listteams Count]) {        returnUitableviewcelleditingstyleinsert; }Else{        returnUitableviewcelleditingstyledelete; }}#pragmaMark Uitableviewdatasource Protocol Method-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{if(Editingstyle = =uitableviewcelleditingstyledelete)        {[Self.listteams removeObjectAtIndex:indexPath.row]; [Self.tableview Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation:    Uitableviewrowanimationautomatic]; }Else{[Self.listteams insertObject:self.txtField.text atindex:[self.listteams Count]]; [Self.tableview Insertrowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation:    Uitableviewrowanimationfade]; } [Self.tableview reloaddata];}#pragmaMark--Uitextfielddelegate delegate method, close keyboard-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{[TextField Resignfirstresponder]; returnYES;}#pragmaMark-Uitextfielddelegate delegate method to avoid the keyboard occlusion text box-(void) Textfielddidbeginediting: (Uitextfield *) TextField {UITableViewCell*cell = (uitableviewcell*) [[TextField Superview] superview]; [Self.tableview Setcontentoffset:cgpointmake (0.0, CELL.FRAME.ORIGIN.Y) animated:yes];}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

4. Mobile Code

-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (nsindexpath*) indexpath{returnYES;}- (void) TableView: (UITableView *) TableView Moverowatindexpath: (nsindexpath*) Sourceindexpath Toindexpath: (Nsindexpath*) destinationindexpath{NSString*stringtomove =[Self.listteams ObjectAtIndex:sourceIndexPath.row];    [Self.listteams RemoveObjectAtIndex:sourceIndexPath.row]; [Self.listteams insertobject:stringtomove atIndex:destinationIndexPath.row];}#pragmaMark--uitableviewdelegate Protocol Method-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath*) indexpath{returnUitableviewcelleditingstylenone;}#pragmaMark-Uiviewcontroller life cycle method for responding to changes in view editing state-(void) Setediting: (BOOL) editing animated: (bool) animated {[Super setediting:editing animated:animated];    [Self.tableview setediting:editing Animated:yes]; }

 

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.