0

I'm new to iOS development. My Main View Controller doesn't display any cells from its table view. I was trying to set it up to display just one cell for now. The main view controller is a subclass of the UIViewController, and has a table view with the prototype cell as well. So my MainViewController.h file looks like below:

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController <UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;

@end

I made the MainVewController a delegate of the UITableViewDataSource, is that the right idea here? My MainViewController.m looks like 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 any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1; //change to 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 what I'm doing wrong here. Shouldn't my MainViewController's Table View be properly displaying the cell? Thoughts?

Vimzy
  • 1,871
  • 8
  • 30
  • 56

3 Answers3

2

You should use in viewDidLoad:

[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
Ritu
  • 661
  • 3
  • 8
  • It says "property 'table view' not found on this object". And also, do I need this: – Vimzy May 15 '15 at 08:24
  • You should have a tableview instance in this main controller. You should use the same instance instead of self.tableView – Ritu May 15 '15 at 08:26
  • The table view will manage the cells. It uses a reuse queue, and recycles/creates cells using their reuse identifiers. – József Vesza May 15 '15 at 08:36
2

I don't see the Table View outlet. Did you forget to connect the Table View from interface builder to your view controller header file? After doing that you should also assign the delegate and data source properties of the table view to "self".

Jorge
  • 1,066
  • 8
  • 16
  • That's right, I forgot to do that. I get that I need to connect the Table View to the interface, but why aren't we required to connect the cell from the table then? A little confused. – Vimzy May 15 '15 at 08:31
  • You don't need to do that, just the table view. The cells are obtained by the reuse identifier and you can access their components programmatically (cell.textLabel = @"text to display";) – Jorge May 15 '15 at 08:37
1

Your class just conforms to <UITableViewDataSource>

you should also conform UITableViewDelegate do it this way.

@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

You missed setting the delegate and dataSource

It can be done in 2 ways:

  1. using code:

    [self.tableView setDelegate:self];
    
    [self.tableView setDataSource:self];
    

put this code in viewDidLoad:

  1. using storyboard: ctrl drag from tableView to your ViewController and set it as delegate and dataSource. see the Image below.

enter image description here

EDIT:

  1. Why don't we need to connect the table's cell as well?

Ans: Table cell is returned from dataSource method tableView:cellForRowAtIndexPath:. This cell is displayed in the tableView. So we don't connect it in the storyboard. However we can configure it in the storyboard.

  1. What's the difference between data source and delegate?

Ans: Delegate: The delegate is an object that is delegated control of the user interface for that event.

Datasource: A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data.

For more information see Delegates and Data Sources and this answer.

Community
  • 1
  • 1
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46