Modify cells-delete, insert, move (IOS)

Source: Internet
Author: User
Tags uikit

Insert and delete timings:

client:setEditing:animated:-----> Set into table View

Table View----> Delegate: (<UITableViewDelegate>) Tableview:editingstyleforrowatindexpath: method to set the cell edit icon

method to set the cell edit icon

Table View----> Data source: (<UITableViewDataSource>) TableView:commiEditingStyle:forRowAtIndexPath message Implementation Delete or insert processing

This is accomplished by clicking the Insert or delete control.

Delete and add cells to reload the table view number by [Self.tableview Reloaddata]!!

Mobile timing:

client:setEditing:animated:-----> Set into table View

Table View---> Delegate: (<UITableViewDelegate>) Tableview:editingstyleforrowatindexpath: method to set the cell edit icon

method to set the cell edit icon

Table View----> Data sources: (<UITableViewDataSource>) Tableview:canmoverowatindexpath messages allow data sources to move cells

Table View----> Delegate: (<UITableViewDelegate>) TableView:moveRowAtIndexPath:toIndexPath: Method Reorder Listteams

The party user drags the sort control to implement.




ViewController.h:

#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller<uitextfielddelegate, Uitableviewdatasource,uitableviewdelegate> @property (Weak, nonatomic) Iboutlet Uinavigationitem *navgationItem;@ Property (weak, nonatomic) Iboutlet UITableView *tableview; @property (Strong, nonatomic) Iboutlet Uitextfield *txtfield; @property (nonatomic, strong) Nsmutablearray *listteams; @end

VIEWCONTROLLER.M:

#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload {[Super V      Iewdidload]; Set the navigation bar//Editbuttonitem is already defined in the view controller, Self.editbuttonitem can get the pointer//Rightbarbuttonitem to the right button of the navigation bar Self.navgationite    M.rightbarbuttonitem = Self.editbuttonitem;        Self.navgationItem.title = @ "cell insertion and deletion";    Set cell text Box Self.txtField.hidden = YES;        Self.txtField.delegate = self;    Assigns the current view controller to the delegate and data source of the table view self.tableView.delegate = self;        Self.tableView.dataSource = self;    Self.listteams = [[Nsmutablearray alloc] initwithobjects:@ "Heilongjiang", @ "Jilin", @ "Liaoning", nil];    }-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma the mark-Uiviewcontroller life cycle method, which responds to changes in the editing state of the view and determines whether it can be entered in edit 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; }} #pragma mark--Uitableviewdatasource Data Source protocol Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection :(Nsinteger) section{//need to add a blank cell return [self.listteams count] + 1;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static Nsstri    ng *cellidentifier = @ "Cell";        BOOL B_addcell;    Determine if the cell needs to be inserted if (Indexpath.row = = self.listTeams.count) {B_addcell = YES;    } else {B_addcell = NO;    }//Build reuse cell UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid    Entifier];  } if (!b_addcell) {//Ordinary cell cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; Set to RIGHT arrow Cell.textLabel.text= [Self.listteams ObjectAtIndex:indexPath.row];        Get text} else{//newly inserted cell//The part can be initialized only when the insert operation is made!        Self.txtField.frame = CGRectMake (10, 0, 300, 44);        Self.txtField.text = @ ""; [Cell.contentview AddSubview:self.txtField]; Implement a text box injection} return cell; #pragma mark-Uitableviewdelegate entrustment Agreement Method-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView  Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{//Edit icon Set//delete or add if (Indexpath.row = = [Self.listteams    Count]) {return uitableviewcelleditingstyleinsert;        } else{return uitableviewcelleditingstylenone;//The reorder control exists at the same time.    return uitableviewcelleditingstyledelete; }}-(BOOL) TableView: (UITableView *) TableView Shouldhighlightrowatindexpath: (Nsindexpath *) indexpath{if (    Indexpath.row = = [Self.listteams count]) {return NO;    In addition to the last line, all highlight} else{return YES; }}-(CGFloat) TableView: (UITableView *) TableView HeightforrowaTindexpath: (Nsindexpath *) indexpath{return 50;} -(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath{return YES; -(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: ( Nsindexpath *) destinationindexpath{//Get target nsstring *stringtomove = [Self.listteams objectatindex:sourceindexpath.    Row];    Remove [Self.listteams RemoveObjectAtIndex:sourceIndexPath.row] from the target location; Re-target location insert data [Self.listteams insertobject:stringtomove atIndex:destinationIndexPath.row];} #pragma mark-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:  Uitableviewrowanimationfade];      [Self.tableview Reloaddata];    //?? } else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {//At least added to Listteams//Add first            Data, and then display [Self.listteams insertObject:self.txtField.text atindex:[self.listteams Count]]; [Self.tableview Insertrowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation:            Uitableviewrowanimationfade];        Reload the data in the table [Self.tableview Reloaddata]; }} #pragma mark-Uitextfielddelegate delegate method, 1. Turn off the keyboard 2. Avoid keyboard occlusion text box-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{/    /recovery keyboard [TextField Resignfirstresponder]; return YES;} -(void) textfielddidbeginediting: (Uitextfield *) textfield{UITableViewCell *cell = (UITableViewCell *) [[TextField Supe    Rview] Superview]; [Self.tableview setcontentoffset:cgpointmake (0.0, CELL.FRAME.ORIGIN.Y) Animated:yes];} @end




Modify cells-delete, insert, move (IOS)

Related Article

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.