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:

  1. using code:

    [self.tableview setdelegate:self];  [self.tableview setdatasource:self]; 

put code in viewdidload:

  1. using storyboard: ctrl drag tableview viewcontroller , set delegate , datasource. see image below.

enter image description here

edit:

  1. 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.

  1. 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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -