Friday, 15 May 2015

ios - Best way to pass button selector from cell to View Controller -



ios - Best way to pass button selector from cell to View Controller -

i have implemented custom implementation of swipeable cell. partly based on :- https://github.com/mbigatti/bmxswipablecell

when basementview setup (this view holds buttons underneth cell content view) add together 2 buttons so:

uibutton *deletebutton = [uibutton buttonwithtype:uibuttontypecustom]; deletebutton.backgroundcolor = [uicolor colorwithred:0.925 green:0.941 blue:0.945 alpha:1.000]; deletebutton.frame = cgrectmake(x + cellheight, 0, cellheight, cellheight); [deletebutton settitle: @"test" forstate: uicontrolstatenormal]; [deletebutton addtarget: self action: @selector(userpressedcallbasementbutton:) forcontrolevents: uicontroleventtouchupinside];

note: called configurecell method, info model passed tableviewcell subclass configure cell. cell has properties iboutlets.

the method userpressedcallbasementbutton implemented in cell subclass, target self. however, info need on view controller, i.e. table info array.

question: how can have target of view controller button , have method on view controller? farther more, how specific cell reference/indexpath used in view controller method too?

first off, indexpath of button, i've subclassed uibutton , given indexpath property can set button when creating cell.

indexedbutton.h

@interface indexedbutton : uibutton @property (nonatomic, retain) nsindexpath *indexpath; @end

sometableviewcontroller.m

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // create cell... cell.deletebutton.indexpath = indexpath; // ... homecoming cell; }

as notifying main view controller, if can't have indexedbutton send action straight view, can either fire off nsnotification or set delegate protocol, whichever preference. delegate/notification send aforementioned indexpath tell main view button pressed.

delegate setup:

customcell.h

#import <uikit/uikit.h> @protocol customcelldelegate; @interface customcell : uitableviewcell // whatever else have in header goes here @property (nonatomic, weak) id<customcelldelegate> delegate; @end @protocol customcelldelegate <nsobject> // other delegate methods go here other buttons need notify main view - (void)callbasementbuttontapped:(nsindexpath *)indexpath; @end

customcell.m

//... - (void)userpressedcallbasementbutton:(id)sender { nsindexpath *indexpath = ((indexedbutton *)sender).indexpath; id<customcelldelegate> strongdelegate = self.delegate; if ([strongdelegate respondstoselector:@selector(callbasementbuttontapped:)]) { [strongdelegate callbasementbuttontapped:indexpath]; } }

meanwhile, in sometableviewcontroller.m

@interface sometableviewcontroller () <customcelldelegate> //... - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // create cell... cell.deletebutton.indexpath = indexpath; cell.delegate = self; // ... homecoming cell; } //... - (void)callbasementbuttontapped:(nsindexpath *)indexpath { // whatever need here, , have index path }

ios objective-c uitableview ios7

No comments:

Post a Comment