IOS Swipe Tableviewcell (left and right swipe to appear button)

Source: Internet
Author: User

1. Background

See QQ left and right sliding Tableviewcell can appear multiple menus, feel very tall on, because there is no such demand,

Also just need a delete button, this system has helped us to achieve, only need to implement a few agents can, make left row

There is a delete button, this I will not say here, Google you should be easy to implement, here is to talk about how to achieve

The implementation of the left and right swipe of multiple buttons. you can get which button is clicked .



The left and right side can be set apart, that is, the picture can be at the same time, the picture side text.

3. Principle of implementation

Inside the cell are buttons and gestures that control the movement and display of the view based on gestures.

4. Customizing the core code of the cell

. h file

#import<UIKit/UIKit.h>#import "menuview.h"typedefenum{cellstateleft, Cellstateright, Cellstatecenter,} cellstate;@protocolMytableviewcelldelegate <nsobject>-(void) Btnmenuclick: (nsinteger) Index Isrightmenu: (Nsinteger) isright;@end@interfaceMytableviewcell:uitableviewcell<uigesturerecognizerdelegate,menuviewdelegate>{cellstate state;} @property (Nonatomic,strong)ID<MyTableViewCellDelegate>mytableviewcelldelegate;- (ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier;- (void) Initleftmenu: (Nsarray *) btnleft Bagcolor: (Nsarray *) Leftcol width: (nsinteger) Leftwid height: (nsinteger) Righthig istext: (BOOL) flag;- (void) Initrightmenu: (Nsarray *) btnright Bagcolor: (Nsarray *) Rightcol width: (nsinteger) Rightwid height: (nsinteger) Righthig istext: (BOOL) flag;@end
View Code

. m file

////MYTABLEVIEWCELL.M//delecttable////Created by Apple on 14/12/12.//Copyright (c) 2014 reasonable. All rights reserved.//#import "myTableViewCell.h"@implementationmytableviewcell{Nsarray*Rightconn; Nsarray*Rightcolor;       Nsinteger Rightwidth;    BOOL Rightistext;    BOOL Hasrightmenu; Nsarray*Leftconn; Nsarray*Leftcolor;    Nsinteger Leftwidth;    BOOL Leftistext;        BOOL Hasleftmenu; Menuview*Leftview; Menuview*Rightview; Nsinteger height;}- (void) initsubcontrol{}- (ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ Self=[Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if(self) {Hasrightmenu=NO; Hasleftmenu=NO; State=Cellstatecenter;    [Self Initcontrol]; }        returnSelf ;}- (void) Initrightmenu: (Nsarray *) btnright Bagcolor: (Nsarray *) Rightcol width: (nsinteger) Rightwid height: (nsinteger) Righthig istext: (BOOL) flag{if((btnright.count>0&& btnright.count==rightcol.count && flag) | | (btnright.count>0&&!flag)) {Rightconn=Btnright; Rightcolor=Rightcol; Rightwidth=[self getmenuwidth:rightwid isright:yes]; Rightistext=Flag; State=Cellstatecenter; Hasrightmenu=YES; Height=Righthig;    [Self Initcontrol]; }}- (void) Initleftmenu: (Nsarray *) btnleft Bagcolor: (Nsarray *) Leftcol width: (nsinteger) Leftwid height: (nsinteger) Righthig istext: (BOOL) flag{if((btnleft.count>0&& btnleft.count==leftcol.count && flag) | | (btnleft.count>0&&!flag)) {Leftconn=Btnleft; Leftcolor=Leftcol; Leftwidth=[self getmenuwidth:leftwid isright:yes]; Leftistext=Flag; State=Cellstatecenter; Hasleftmenu=YES; Height=Righthig;    [Self Initcontrol]; }}-(Nsinteger) Getmenuwidth: (Nsinteger) fucwidth isright: (BOOL) flag{ Nsinteger totalwidth=flag?rightconn.count*rightwidth:leftconn.count *Leftwidth; Nsinteger Muennumber=flag?Rightconn.count:leftconn.count; if(totalwidth>self.frame.size.width) {return(self.frame.size.width/muennumber); }Else{        returnFucwidth; }  }- (void) initcontrol{if(hasrightmenu) {Rightview=[[menuview Alloc]initwithframe:cgrectmake (self.frame.size.width-(Rightwidth*rightconn.count),0, rightwidth*Rightconn.count,height)];        [Rightview initmenu:rightconn Bagcolor:rightcolor width:rightwidth Istext:rightistext]; Rightview.menuviewdelegate=Self ; Rightview.hidden=YES;            [Self addsubview:rightview]; }    if(hasleftmenu) {Leftview=[[menuview Alloc]initwithframe:cgrectmake (0,0, leftwidth*Leftconn.count,height)];        [Leftview initmenu:leftconn Bagcolor:leftcolor width:leftwidth Istext:leftistext]; Leftview.menuviewdelegate=Self ; Leftview.hidden=YES;    [Self addsubview:leftview]; } self.userinteractionenabled=YES; Self.contentView.userInteractionEnabled=YES; Self.contentView.backgroundColor=[Uicolor Whitecolor]; Uiswipegesturerecognizer* sc1=[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (handleswipe:)]; SC1.Delegate=Self ;    [Sc1 Setdirection:uiswipegesturerecognizerdirectionleft]; Uiswipegesturerecognizer* sc2=[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (handleswipe:)]; SC2.Delegate=Self ;    [SC2 setdirection:uiswipegesturerecognizerdirectionright];    [Self.contentview ADDGESTURERECOGNIZER:SC1];    [Self.contentview ADDGESTURERECOGNIZER:SC2];    [Self bringSubviewToFront:self.contentView]; }- (void) Btnclick: (Nsinteger) index{[self.mytableviewcelldelegate btnmenuclick:index isrightmenu:state];}-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch{//Judging is not UIButton class    if([Touch.view Iskindofclass:[uibuttonclass]])    {        returnNO; }    Else    {        returnYES; }}-(void) Handleswipe: (Uiswipegesturerecognizer *) Gesture {Switch(gesture.direction) { CaseUiswipegesturerecognizerdirectionup:nslog (@"%@",@" up");  Break;  CaseUiswipegesturerecognizerdirectiondown: Break;  Caseuiswipegesturerecognizerdirectionleft: {if(state==cellstatecenter) {Rightview.hidden=NO; CGRect RCT=CGRectMake (self.contentview.frame.origin.x, SELF.CONTENTVIEW.FRAME.ORIGIN.Y,                    Self.contentView.frame.size.width, Self.contentView.frame.size.height); Rct.origin.x=rct.origin.x-rightwidth*[Rightconn Count]; Self.contentView.frame=RCT; State=Cellstateright; }                                                if(state==cellstateleft) {Leftview.hidden=YES; CGRect RCT=CGRectMake (self.contentview.frame.origin.x, SELF.CONTENTVIEW.FRAME.ORIGIN.Y,                    Self.contentView.frame.size.width, Self.contentView.frame.size.height); Rct.origin.x=rct.origin.x-leftwidth*[Leftconn Count]; Self.contentView.frame=RCT; State=Cellstatecenter; }                                            }                 Break;  Caseuiswipegesturerecognizerdirectionright: {if(state==cellstatecenter) {Leftview.hidden=NO; CGRect RCT=CGRectMake (self.contentview.frame.origin.x, SELF.CONTENTVIEW.FRAME.ORIGIN.Y,                    Self.contentView.frame.size.width, Self.contentView.frame.size.height); Rct.origin.x=rct.origin.x+leftwidth*[Leftconn Count]; Self.contentView.frame=RCT; State=Cellstateleft; }                                if(state==cellstateright) {Rightview.hidden=YES; CGRect RCT=CGRectMake (self.contentview.frame.origin.x, SELF.CONTENTVIEW.FRAME.ORIGIN.Y,                  Self.contentView.frame.size.width, Self.contentView.frame.size.height); Rct.origin.x=rct.origin.x+rightwidth*[Rightconn Count]; Self.contentView.frame=RCT; State=Cellstatecenter; }                           }                 Break; default:                 Break; }    }- (void) Rightviewselectindex: (UIButton *) btn{NSLog (@"%ld", Btn.tag);}- (void) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated]; //Configure The View for the selected state}@end
View Code

5. Description

The development environment is the effect of Xcode 6.1 SDK8.1 not testing other versions, and if you need to test dome leave the mailbox, I'll send it to you.

Welcome reprint.

IOS Swipe Tableviewcell (left and right swipe to appear button)

Related Article

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: 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.