Wednesday 15 February 2012

uitableview - Managing UIPickerView datasource between a UIVIewController, a custom UITableViewCell and a Utility Class -



uitableview - Managing UIPickerView datasource between a UIVIewController, a custom UITableViewCell and a Utility Class -

i have custom uitableviewcell class contains picker. cell , it's contents populated core info entity. since custom cell viewable in more 1 view controller, have utility class want have handle construction, datasource , delegate methods.

i seem missing when set datasource self, cell displays properly. when set utility class, numberofcomponentsinpickerview: method gets called , app crashes nil in log except (lldb). code sample, i'm going create simple picker 1 component , 1 row title "test".

viewcontroller.m - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { pickercell *pcell = nil; picker *picker = [self.array objectatindex:indexpath.row]; utilitycustomcells *cellutility = [[utilitycustomcells alloc]init]; pcell = [cellutility createpickercell:tableview withreuseidentifier:kpickercellrid andpicker:picker]; pcell.picker.delegate = cellutility; //does not work pcell.picker.datasource = cellutility; //does not work /* works pcell.picker.delegate = self; pcell.picker.datasource = self; */ homecoming pcell; } }

and in

utilitycustomcells.h #import "pickercell.h" #import "picker.h" @protocol customcellutilitydelegate <nsobject,uipickerviewdatasource,uipickerviewdelegate> -(nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview; -(nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component; -(nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component; @end

finally:

utilitycustomcells.m -(nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component { nslog(@"here1"); homecoming 1; } -(nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview { nslog(@"here2"); homecoming 1; } -(nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component { nslog(@"here3"); nsstring *title = @"test"; homecoming title; }

just re-iterate, "here2" gets printed logs. advice much appreciated. in advance!

i able figure out. instantiating utilitycustomcell class within cellforrowatindexpath datasource method in viewcontroller.m. in fact, needed create instance variable of viewcontroller.m , alloc/init in viewdidload. now:

viewcontroller.m @interface assetformvc () { utilitycustomcells *cellutility; } @end - (void)viewdidload { [super viewdidload]; cellutility = [[utilitycustomcells alloc]init]; cellutility.pickerdatasource = [self configurepickerdata]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { pickercell *pcell = nil; picker *picker = [self.array objectatindex:indexpath.row]; pcell = [cellutility createpickercell:tableview withreuseidentifier:kpickercellrid andpicker:picker]; pcell.picker.delegate = cellutility; pcell.picker.datasource = cellutility; homecoming pcell; } }

i can send dictionary picker datasource cellutility via property in utilitycustomcells.h

@property (nonatomic,strong) nsdictionary *pickerdatasource;

uitableview uipickerviewdatasource

No comments:

Post a Comment