IOS UITableViewCell "Swipe to appear multiple buttons"

Source: Internet
Author: User

Preface: This blog is actually want to introduce Tableviewcell sliding some "things", yesterday in the trip to GitHub saw a very interesting three-party library, simple with a feel good, a record, the second is hoping to have similar needs can be helped.
This article describes the implementation of the IOS8 (System method) after IOS5 (using a three-party library)

IOS > = 5.0

ios> = 8.0

Mgswipetablecell (three-party library on GitHub)-IOS >= 5.0

Direct use comparison simple to look through the code

首先签这个协议MGSwipeTableCellDelegate

Add Left button method
- (Nsarray*) Btnleftcount: (int) count{Nsmutablearray*result = [NsmutablearrayArray];Uicolor*colors[3] = {[UicolorGreencolor], [UicolorColorwithred:0Green0x99/255.0Blue0XCC/255.0Alpha1.0],        [UicolorColorwithred:0.59Green0.29Blue0.08Alpha1.0]};; for(inti =0; I < count; i + +) {The//button provides several ways to take a look inside .Mgswipebutton *btn = [Mgswipebutton buttonwithtitle:@""Backgroundcolor:colors[i] padding: thecallback:^BOOL(Mgswipetablecell *sender) {return YES; }];//Add the button to the array[Result ADDOBJECT:BTN]; }returnResult;}
Ways to add the right button
- (Nsarray*) Btnrightcount: (int) count{Nsmutablearray*result = [NsmutablearrayArray];Nsarray*titlearray = @[@"Delete", @"Mark Unread"];Uicolor*color[2] = {[UicolorRedcolor], [UicolorLightgraycolor]}; for(inti =0; I < count; i + +) {Mgswipebutton *btn = [Mgswipebutton buttonwithtitle:titlearray[i] backgroundcolor:color[i] padding: thecallback:^BOOL(Mgswipetablecell *sender) {BOOLAutoHide = i! =0;returnAutoHide; }];//Add the button to the array[Result ADDOBJECT:BTN]; }returnResult;}
Reuse pools can be written like this
- (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) indexpath{Static NSString*cellid = @"Cellid";//Here if Mgswipetablecell is enough for you to use, you can use it directly    //or custom create cell inherits from Mgswipetablecell, like my code belowXtcustomcell *cell = [TableView dequeuereusablecellwithidentifier:cellid];if(!cell)    {cell = [[Xtcustomcell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellid]; } cell. Label. Text= [NSStringstringwithformat:@"%ld------%@", (Long) Indexpath. Row, Self. Arraytest[Indexpath. Row]]; Cell. Label. Font= [UifontSystemfontofsize: -];//Designated AgentCell. Delegate= Self;//NO: Only a single can slide, YES: multiple can slideCell. Allowsmultipleswipe=NO;returnCell;}
Add button
-(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction             swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;{    if (direction == MGSwipeDirectionRightToLeft) {        return [self btnRightCount:2];    }    else {        return [self btnLeftCount:3];    }}
Button's Click Proxy method
-(BOOL) Swipetablecell: (mgswipetablecell*) cell Tappedbuttonatindex: (Nsinteger) Index Direction: (mgswipedirection) Direction fromexpansion: (BOOL) fromexpansion{Switch(direction) { CaseMgswipedirectionlefttoright: {if(Index = =0) {NSLog(@"right-------0"); }Else{NSLog(@"right-------1"); } Break; } CaseMgswipedirectionrighttoleft: {if(Index = =0) {NSLog(@"left-------0");//Here is a simple delete operation                Nsindexpath* Path = [_tableview Indexpathforcell:cell]; [_arraytest Removeobjectatindex:path. Row]; [_tableview Deleterowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationleft];return NO; }Else{NSLog(@"left-------1"); } Break; }    }return YES;}
A similar implementation is also provided after the IOS8
- (Nsarray<uitableviewrowaction *> *) TableView: (UITableView*) TableView Editactionsforrowatindexpath: (Nsindexpath*) indexpath{uitableviewrowaction *deleterowaction = [uitableviewrowaction rowactionwithstyle: Uitableviewrowactionstyledestructive title:@"Delete"handler:^ (uitableviewrowaction * _nonnull action,Nsindexpath* _nonnull Indexpath) {[ Self. ArraytestRemoveobjectatindex:indexpath. Row]; [ Self. TableViewDeleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];    }]; Uitableviewrowaction *toprowaction = [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledefault title:@"Sticky"handler:^ (uitableviewrowaction * _nonnull action,Nsindexpath* _nonnull Indexpath) {[ Self. ArraytestExchangeobjectatindex:indexpath. RowWithobjectatindex:0];Nsindexpath*firstindexpath = [NsindexpathIndexpathforrow:0Insection:indexpath. section];    [TableView Moverowatindexpath:indexpath Toindexpath:firstindexpath];    }]; Toprowaction. BackgroundColor= [UicolorBluecolor]; Uitableviewrowaction *morerowaction = [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"More"handler:^ (uitableviewrowaction * _nonnull action,Nsindexpath* _nonnull Indexpath) {[ Self. TableViewReloadrowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowanimationmiddle]; }];return@[deleterowaction,toprowaction,morerowaction];}
Weibo @ Summer is an adult, and you're welcome to follow me. You can also join me to create a technical Exchange group: 498143780 to communicate with me. Study together, give a praise to encourage.

IOS UITableViewCell "Swipe to appear multiple buttons"

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.