UITableView Editable State Common operations

Source: Internet
Author: User

turn from: http://blog.csdn.net/ljloving/article/details/7955153


1, marking line

The tag line here refers to clicking this row to achieve a tick on the right side of this row, as shown in the following illustration:

To implement the markup feature, add code before @end in VIEWCONTROLLER.M:

#pragma mark-#pragma mark Table Delegate Methods-(void) TableView: (UITableView *) TableView Didselectrowatindexpath:       (Nsindexpath *) Indexpath {UITableViewCell *onecell = [TableView Cellforrowatindexpath:indexpath]; if (Onecell.accessorytype = = uitableviewcellaccessorynone) {Onecell.accessorytype = Uitableviewcellaccessoryche       Ckmark;       else Onecell.accessorytype = Uitableviewcellaccessorynone;    [TableView Deselectrowatindexpath:indexpath Animated:yes]; }

This code implements: When a row is clicked, the line is marked if it is not marked, and if the row is marked, the tag is canceled.

The operation effect is as shown above.

The code above actually modifies the Accessorytype property of a row, which can be set to four constants:

Uitableviewcellaccessorycheckmark
Uitableviewcellaccessorydetaildisclosurebutton
Uitableviewcellaccessorydisclosureindicator
Uitableviewcellaccessorynone


Uitableviewcellaccessorycheckmark Uitableviewcellaccessorydetaildisclosurebutton

Uitableviewcellaccessorydisclosureindicator Uitableviewcellaccessorynone

2, move the line

To move or delete rows like this, you need to start the edit mode of the table. The setediting:animated is used: method.

2.1 Opens the Viewcontroller.xib and maps the table controls to outlet to ViewController.h, named Mytableview.

2.2 Open VIEWCONTROLLER.M, at the end of the Viewdidload method add code://Start table edit mode [Self.mytableview setediting:yes animated:yes]; 2.3 Add code before @end:

When editing mode is turned on, the Red Delete button appears on the left side of each line by default, which is to turn off the button's   -  (Uitableviewcelleditingstyle) TableView: ( uitableview *) tableview               Editingstyleforrowatindexpath: (nsindexpath *) indexpath {         return uitableviewcelleditingstylenone;   }      //This method is used to tell the form   Whether this line can be moved   -  (BOOL) TableView: (uitableview *) Tableview canmoverowatindexpath: ( nsindexpath *) indexpath {        return YES;    }     /This method is to perform the mobile operation of the   -  (void) TableView: (uitableview *) TableView  moverowatindexpath: (nsindexpath *)             Sourceindexpath toindexpath: (nsindexpath *) destinationindexpath {        nsuinteger fromrow = [sourceindexpath row];        nsuinteger torow  = [destinationIndexPath row];                id object = [list objectAtIndex:fromRow];         [list removeObjectAtIndex:fromRow];        [list  insertobject:object atindex:torow];   }   Editingstyleforrowatindexpath This method uses the constant Uitableviewcelleditingstylenone, which means that it is not editable, where the edit refers to the deletion and insertion. Constants that represent the editing mode of a table row are:

Uitableviewcelleditingstyledelete Uitableviewcelleditingstyleinsert Uitableviewcelleditingstylenone

As the name suggests, the first represents the deletion, the second represents the insertion, and the third is not editable.

If the Uitableviewcelleditingstylenone in the Editingstyleforrowatindexpath method is replaced by the above three values, the effect they run is shown in the following illustration:


2.4 Running, you can see from the following figure that you have implemented the movement of the row:


However, you will find that you cannot mark each row at this time. This means that in edit mode, the row cannot be selected, thus didselectrowatindexpath this method does not execute.

3, delete line

From the 2nd step, to achieve the deletion of a row, in fact, relatively simple.

3.1 Modify the Uitableviewcelleditingstylenone in the Editingstyleforrowatindexpath method into Uitableviewcelleditingstyledelete. 3.2 Add code before @end:

//This method is based on the parameter editingstyle is uitableviewcelleditingstyledelete  // or uitableviewcelleditingstyledelete execution Delete or insert   -  (void) TableView: (uitableview *) TableView  commitEditingStyle:        (Uitableviewcelleditingstyle) editingstyle  Forrowatindexpath: (nsindexpath *) indexpath {       if  ( Editingstyle == uitableviewcelleditingstyledelete)  {            NSUInteger row = [indexPath row];             [self.list removeObjectAtIndex:row];             [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath]                 &NB

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.