Ios-uitableview edit (cell insert, delete, move)

Source: Internet
Author: User

Insert/delete of UITableView cell

Core APS I

Class : uitableview
Delegate : uitableviewdatasource, uitableviewdelegate
API involved: (see the end of this chapter for an official detailed comment on the API)

/** TableView Enter or exit the edit state (TableView method). */-(void) setediting: (BOOL) editing animated: (BOOL) animate/** determines which rows of cells can be edited (methods in Uitableviewdatasource protocol). */-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath/** Set the edit mode for a row of cells ( Uitableviewdelegate protocol method). */tableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath/** commits the edit state (method in the Uitableviewdatasource protocol). */-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath/** Insert cell (UITableView method). */-(void) Insertrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation/** Delete Cell (UITableView method). */-(void) Deleterowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation


function implementation
Ideas:

1. Let the tableview into the edit state 2. Specify which cells can be edited 3. Specify the cell's edit state (delete or Insert) 4. Select Actions after delete (or insert) status (data source for update, cell delete or insert)

Code:
1. Let TableView into edit state (UIVIEWCONTROLL.M)

/** This method is called when you click on the Uinavigationbar system-provided edit button. */-(void) setediting: (BOOL) editing animated: (BOOL) animated{    /** First calls the method of the parent class. */    [Super Setediting:editing Animated:animated];    /** make TableView in edit state. */    [Self.tableview setediting:editing animated:animated];}

2. Specify which rows of cells can be edited (Uitableviewdatasource protocol method)

-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{    if (0 = = Indexpath.row ) {        return NO;  /**< the first line cannot be edited. */    } else {         return YES;}    }

3. Specify the edit state of the cell (delete or insert) (Uitableviewdelegate protocol method)

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{    /** Different lines, you can set different editing styles, the edit style is an enumeration type *    /if (Indexpath.row = = 0) {        return Uitableviewcelleditingstyleinsert;     } else {        return uitableviewcelleditingstyledelete;}    }

4. Check actions after delete (or insert) status (data source for update, cell delete or insert) (Uitableviewdatasource protocol method)

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{    /**   Click the Delete button *    /if (Editingstyle = = Uitableviewcelleditingstyledelete) {/**< determines when the edit state is deleted. *        //** 1. Update data source (array): Deletes data from the array according to the Indexpaht.row as an array subscript. */< C4/>[self.arr RemoveObjectAtIndex:indexPath.row];         /** 2. Delete a cell in the TableView. */        [TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationright];    }    /** Click the + sign icon to operate. *    /if (Editingstyle = = Uitableviewcelleditingstyleinsert) {/**< determines when the edit state is inserted. */                /** 1. Update the data source: Add data to the array. */< C11/>[self.arr insertobject:@ "ABCD" atIndex:indexPath.row];        /** 2. Insert a cell into the tableview. */        [TableView Insertrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];    }}

UITableView Cell's Movement

Core API

Class: uitableview
deletage: uitableviewdatasource, uitableviewdelegate
An official detailed comment on the API:(API involved is described at the end of this chapter)

/** TableView Enter or exit the edit state (TableView method). */-(void) setediting: (BOOL) editing animated: (BOOL) animate/** specifies tableView which rows (cells) can be moved. */-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath/** move cell. */-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: ( Nsindexpath *) Destinationindexpath

function implementation

Ideas:

1. Let TableView enter or exit the Edit state 2. Specify TableView which rows (cells) can be moved 3. Actions after cell move: data source for update
Code

1. Let TableView enter or exit the edit state

/** This method is called when you click on the Uinavigationbar system-provided edit button. */-(void) setediting: (BOOL) editing animated: (BOOL) animated{    /** First calls the method of the parent class. */    [Super Setediting:editing Animated:animated];    /** make TableView in edit state. */    [Self.tableview setediting:editing animated:animated];}

2. Specify TableView which rows (cells) can be moved (Uitableviewdatasource protocol method)

-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath{    /** Specify which lines (cells) can be moved * /    if (0 = = Indexpath.row) {        return NO;  /**< no cell cannot move */    } else {        return YES;/**< Yes cell can move */    }}

3. Actions after moving the cell: data source for update

-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: ( Nsindexpath *) destinationindexpath{    /**  1. Remove from the original location, before removing from the original location, you need to save the original location of the data, and hold it once. */    NSString *str = [[ Self.arr ObjectAtIndex:sourceIndexPath.row] retain];    [Self.arr RemoveObjectAtIndex:sourceIndexPath.row];    /** 2. Add to the destination and release once */    [Self.arr insertobject:str atIndex:destinationIndexPath.row];    [str release];}

API Official comments

/** * @brief asks the data source to commit the insertion or deletion of a specifie D row in the receiver. * * @param <tableView> the Table-view object requesting the insertion or deletion. * @param <editingStyle> the cell editing style corresponding to a insertion or deletion requested for the row spe Cified by Indexpath. Possible editing styles are Uitableviewcelleditingstyleinsert or uitableviewcelleditingstyledelete. * @param <indexPath> an index path locating the row in TableView. */-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath 
/** * @brief asks the data source to verify that the given row is editable. * * @pa Ram <tableView> the Table-view object requesting this information. * @param <indexPath> an index path locating a row in TableView. * * @return YES if the row indicated by Indexpath is editable; Otherwise, NO. */-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath 
/** * @brief inserts rows in the table view at the locations identified by an array of index paths, with a option to animate the insertion. * * @param <indexPaths> An array of Nsindexpath objects, each representing a row index and sections index that T Ogether identify a row in the table view.  * @param <animation> A constant that either specifies the kind of animation-perform when inserting the cell or requests no animation. See Table Cell insertion and deletion Animation for descriptions of the constants. * **/-(void) Insertrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation 
/** * @brief   deletes the rows specified by a array of index paths, with a option to animate the deletion. *  * @p Aram   <indexPaths> An    arrays of Nsindexpath objects identifying the rows to delete. * @param   <animatio N>     A constant that indicates how the deletion are to being animated, for example, fade out or slide out from the bottom . See Table Cell insertion and deletion Animation for descriptions of these constants. * */-(void) Deleterowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation

Ios-uitableview edit (cell insert, delete, move)

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.