Tuesday, 15 January 2013

iphone - Separating UITableView's delegate/datasource from main ViewController -



iphone - Separating UITableView's delegate/datasource from main ViewController -

i'm quite new ios development. have read issues viewcontrollers becoming huge , follow design have seen in reply previous question uitableview issue when using separate delegate/datasource app has 2 different tables , couple of buttons on 1 screen. somehow confused in storyboard connections create between testtableviewcontroller , testtabletestviewcontroller.

can provide sample working project or screen shots on how connect uitableview delegate, info source , connecting outlet separate custom uiviewcontroller subclass (testtabletestviewcontroller) in storyboard please?

also, design work xcode 5 / ios 7 , above?

you have create class named mytableviewdatasource , implement tabbleviewdatasource methods in it. create property of info source class. set info source in viewcontroller here examle:

@interface mytableviewdatasource () @property (nonatomic, assign) int sectiononerows; @property (weak) id<yourdelegate> delegate; @property (nonatomic, strong) nsarray *datasourcearray; @end @implementation mytableviewdatasource @synthesize delegate=_delegate; -(id) initdatasourcewithdarray:(nsarray *)array delegate:(id)delegate { if (self = [super init]) { self.datasourcearray=array; self.delegate = delegate; } homecoming self; } #pragma mark - uitableviewdatasource - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming numberofrows; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *celltoreturn = nil; //your code here homecoming celltoreturn } #pragma mark - uitableview delegate - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { [tableview deselectrowatindexpath:indexpath animated:yes]; //your logic here //implement delegate method in viewcontroller [self.delegate performselector:@selector(loaddetailsatindexpath:)withobject:indexpath]; } }

in viewcontroller:

self.mydatasource = [[mytableviewdatasource alloc] initdatasourcewithdarray:self.dataarray delegate:self]; [self.mytableview setdatasource:self.mydatasource]; [self.mytableview setdelegate:self.mydatasource]; [self.mytableview reloaddata];

your delegate method:

-(void)loaddetailsatindexpath:(nsindexpath *)indexpath { mydetailviewcontroller *mydetailcontroller = [[mydetailviewcontroller alloc] initwithnibname:@"mydetailviewcontroller" bundle:nil]; //your logic here [self.navigationcontroller pushviewcontroller:mydetailcontroller animated:yes]; }

go connections inspectors , create changes this:

data source tableview set programatically here rather in ib. hope help you.

iphone objective-c cocoa-touch uitableview

No comments:

Post a Comment