The implementation principle and deletion of uitableview editing

Source: Internet
Author: User

A. The process of execution

1,tableview into edit state

The user clicks on a button to let the program enter the editing state,

self.tableView.editing = YES;

2, ask TableView if the cell can be edited

TableView asks the DataSource agent to execute an agent method asking for the edit status of each line

i.e.-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath//default Yes, i.e. at editing = yes, Default all rows can be deleted

3, ask TableView's edit type (delete or add)

TableView asks the editor status of each line of the delegate agent

i.e.-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath//Delete by default

Where Uitableviewcelleditingstyle is an enumeration type

typedef Ns_enum(Nsinteger, Uitableviewcelleditingstyle) {

Uitableviewcelleditingstylenone,

Uitableviewcelleditingstyledelete,

Uitableviewcelleditingstyleinsert

The};//system comes with three editing states

typedef ns_enum (Nsinteger, Uitableviewcelleditingstyle) {

Uitableviewcelleditingstylenone,

Uitableviewcelleditingstyledelete,

Uitableviewcelleditingstyleinsert

};

4,tableview Response Edit Operation

A proxy method is executed by DataSource to respond to an edit operation

i.e.-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath

Specific instructions

-(void) TableView: (uitableview *) TableView Commiteditingstyle: (Uitableviewcelleditingstyle ) Editingstyle Forrowatindexpath: (nsindexpath *) indexpath

{

if (Editingstyle = = Uitableviewcelleditingstyledelete) {// Perform delete operation

//1, First delete the data from the data source

//2. Visually removed, the data must be actually removed from the data source before it is deleted, or the program will crash (the first parameter in the following method is the nsarray type, which identifies the row to be deleted , and the element that holds the Nsindexpath object)

[_mytableview deleterowsatindexpaths: @[indexpath] withrowanimation: Uitableviewrowanimationleft];

// Note that the order of execution is not variable

/**

* Delete the entire section (logic is similar to deleting a row in the sections )

*/

//1, First delete data from the data source

//2, visually removed ( the first parameter in the following method is of type Nsindexset, which identifies the section to be deleted )

[_mytableview deletesections: [nsindexset indexsetwithindex: Indexpath. Section] withrowanimation:uitableviewrowanimationleft];

}

if (Editingstyle = = Uitableviewcelleditingstyleinsert) {// Perform add operation

///1, Add data from the data source location

///2, added visually (the first parameter of the following method is the established add position )

[_mytableview insertrowsatindexpaths: @[indexpath] withrowanimation:     Uitableviewrowanimationleft]; // Add the current position at the point you clicked

//Customizable Nsindexpath object to make insertion position

}

}

The implementation principle and deletion of uitableview editing

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.