objective c - Custom accessoryView image and accessoryButtonTappedForRowWithIndexPath -
i trying custom image working accessorybuttontappedforrowwithindexpath
function. understand in order this, have use uibutton
, adding uiimageview
on uitableviewcellaccessorydetaildisclosurebutton
not work.
my problem how touch gesture uitableviewcell
subclass parent uitableview
function.
here code in uitableviewcell
subclass:
upvote = [uibutton buttonwithtype:uibuttontypecustom]; uiimage *upvoteimg = [uiimage imagenamed:@"vote.png"]; upvote.frame = cgrectmake(self.frame.size.width-upvoteimg.size.width, 0, upvoteimg.size.width , upvoteimg.size.height); [upvote setbackgroundimage:upvoteimg forstate:uicontrolstatenormal]; [self.contentview addsubview:upvote]; [upvote addtarget:self action:@selector(checkbuttontapped:event:) forcontrolevents:uicontroleventtouchupinside];
the calling function (also inside subclass of uitableviewcell
)
- (void)checkbuttontapped:(id)sender event:(id)event { uitableview *tableview = (uitableview *)self.superview; [tableview.delegate tableview:tableview accessorybuttontappedforrowwithindexpath:[tableview indexpathforcell:self]]; }
it crashes @ final line of above function this:
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uitableviewwrapperview delegate]: unrecognized selector sent instance 0x16f48160'
yes should crash, because delegate of tableview should not called custom cell class, better can use custom delegate. posting sample code, similar case, when button tapped call's custom delegate method u can whatever want
//in custom calss #import <uikit/uikit.h> //create delegate @protocol customdelegate<nsobject> -(void)whenbuttontapped:(id)sender; //your delegate method @end @interface customcell : uitableviewcell<customdelegate> @property(nonatomic, assign)id<customdelegate> delegate; //create delegate @end //.m file of custom cell #import "customcell.h" @implementation customcell @synthesize delegate; //sysnthesize delegate - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // initialization code uibutton *abutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; //its button uilabel *alabel = [[uilabel alloc]initwithframe:cgrectzero]; //for example took label [abutton addtarget:self action:@selector(whenbuttontapped:) forcontrolevents:uicontroleventtouchupinside];//adding target button action [abutton settag:100]; [alabel settag:200]; [self addsubview:abutton]; [self addsubview:alabel]; //since using without arc [alabel release]; } return self; } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } -(void)dealloc { delegate = nil; [super dealloc]; } //i setting frames hear -(void)layoutsubviews { [super layoutsubviews]; uibutton *abutton = (uibutton *)[self viewwithtag:100]; //get button abutton.frame = cgrectmake(200, 5, 55, 40); [abutton settitle:@"tapme" forstate:uicontrolstatenormal]; uilabel *label = (uilabel *) [self viewwithtag:200]; //get label label.frame = cgrectmake(40, 5, 50, 35); label.text = @"hello"; } //hear ur button's target - (void)whenbuttontapped:(id)sender { //dont call uitableview delegate method in custom cell wrong, ur app crashed //insted create custom delegate method controller [self.delegate whenbuttontapped:sender];//call delegate method when button tapped } @end //.h file u using custom cell #import <uikit/uikit.h> #import "customcell.h" @interface viewcontroller : uiviewcontroller<uitableviewdatasource , uitableviewdelegate ,customdelegate>//set confirms delegate @property (retain, nonatomic) iboutlet uitableview *atableview; @end //.m file -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { customcell *acell = [self.atableview dequeuereusablecellwithidentifier:@"cell"]; if(acell == nil) { acell = [[customcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cell"]; } acell.delegate = self;//set delegate class return acell; } //hear delegate method //define delegate method hear -(void)whenbuttontapped:(uibutton *)sender { //in method u can ever activity when button tapped //sender gives u entire cell information uitableviewcell *cell = sender.superview;//you can cell :) nslog(@"button tapped"); }
hopes helps
Comments
Post a Comment