ios - Making Main View Controller Delegate of UITableViewDataSource -
i'm new ios development. main view controller doesn't display cells table view. trying set display 1 cell now. main view controller subclass of uiviewcontroller, , has table view prototype cell well. mainviewcontroller.h file looks below:
#import <uikit/uikit.h> @interface mainviewcontroller : uiviewcontroller <uitableviewdatasource> @property (weak, nonatomic) iboutlet uibarbuttonitem *sidebarbutton; @end
i made mainvewcontroller delegate of uitableviewdatasource, right idea here? mainviewcontroller.m looks below:
#import "mainviewcontroller.h" #import "swrevealviewcontroller.h" @interface mainviewcontroller () @end @implementation mainviewcontroller - (void)viewdidload { [super viewdidload]; self.title = @"home"; swrevealviewcontroller *revealviewcontroller = self.revealviewcontroller; if(revealviewcontroller) { [self.sidebarbutton settarget: self.revealviewcontroller]; [self.sidebarbutton setaction: @selector(revealtoggle:)]; [self.view addgesturerecognizer:self.revealviewcontroller.pangesturerecognizer]; } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 1; //change number of post objects in array (array.count) } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"basiccell" forindexpath:indexpath]; return cell; } @end
i don't understand i'm doing wrong here. shouldn't mainviewcontroller's table view displaying cell? thoughts?
your class conforms <uitableviewdatasource>
you should conform uitableviewdelegate
way.
@interface mainviewcontroller : uiviewcontroller <uitableviewdatasource, uitableviewdelegate>
you missed setting delegate
, datasource
it can done in 2 ways:
using code:
[self.tableview setdelegate:self]; [self.tableview setdatasource:self];
put code in viewdidload:
- using storyboard: ctrl drag
tableview
viewcontroller
, setdelegate
,datasource
. see image below.
edit:
- why don't need connect table's cell well?
ans: table cell returned datasource
method tableview:cellforrowatindexpath:
. cell displayed in tableview
. don't connect in storyboard. can configure in storyboard.
- what's difference between data source , delegate?
ans: delegate: delegate object delegated control of user interface event.
datasource: data source delegate except that, instead of being delegated control of user interface, delegated control of data.
for more information see delegates , data sources , this answer.
Comments
Post a Comment