IPhone: editable tableview Move & Delete

Source: Internet
Author: User

Set a button, click it to delete it, and move the sorted tableview as follows: move on the left, delete on the right

Move first:

Button to set ibaction

-(Ibaction) togglemove {[self. tableview setediting:!Self. tableview. Editing animated: Yes];If(Self. tableview. Editing) [self. navigationitem. rightbarbuttonitem settitle:@"Done"];Else[Self. navigationitem. rightbarbuttonitem settitle:@"Move"];}

Self.Tableview.The editing attribute is of the bool type, indicating the editing status of tableview.

Here, the button is set to two types of text, corresponding to different States.

Then implement three delegate methods:

-(Uitableviewcelleditingstyle) tableview :( uitableview * ) Tableview editingstyleforrowatindexpath :( nsindexpath * ) Indexpath {  Return  Uitableviewcelleditingstylenone ;} -(Bool) tableview :( uitableview * ) Tableviewcanmoverowatindexpath :( nsindexpath * ) Indexpath {  Return  Yes ;} -( Void ) Tableview :( uitableview *) Tableviewmoverowatindexpath :( nsindexpath * ) Fromindexpath toindexpath :( nsindexpath * ) Toindexpath {nsuinteger fromrow = [Fromindexpath row]; nsuinteger torow = [Toindexpath row];  ID   Object = [List objectatindex: fromrow]; [list removeobjectatindex: fromrow]; [list insertobject:  Object  Atindex: torow];} 

Deleting is simpler.

The action of the button is the same as the preceding one,

You only need to implement one delegate method:

 
-(Void) Tableview :( uitableview *) Tableviewcommiteditingstyle :( uitableviewcelleditingstyle) editingstyleforrowatindexpath :( nsindexpath*) Indexpath {nsuinteger row=[Indexpath row]; [self. List removeobjectatindex: Row]; [tableview deleterowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationautomatic];}

 

You can also combine the two to implement the above 4-brother delegation method. Note that in the editingstyleforrowatindexpath method, uitableviewcelleditingstyledelete is returned.

As follows:

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.