monkey man
monkey man

Reputation: 51

tableview is connected to datasource and delegate - how

i am new to iOS, this may seem like a basic question. I have been working through this tutorial and I have no idea how the tableview is connected to the code. The sample project can be downloaded here.

I was my understanding that you need to extend UITableViewDelegate, UITableViewDataSource in the code, then in the storyboard you can drag from the tableview to them.

But what is perplexing is that the sample project does not extend UITableViewDelegate, UITableViewDataSource at all, therefore, how is the tableview in the story board connected to the code ?

Upvotes: 2

Views: 1703

Answers (6)

Tapas_ios
Tapas_ios

Reputation: 73

In viewcontroller.h file e declare the delegate and datasource method.

UIViewController<UITableViewDataSource,UITableViewDelegate>

then connect the delegate and datasource method with viewController so in viewController.m file

- (void)viewDidLoad {
tableview.delegate=self;
tableview.dataSource=self;
}

Upvotes: 1

Stefan
Stefan

Reputation: 1496

In Objective-C/iOS you often implement things by having classes conform to protocols rather than subclassing classes. WTTableViewController conforms to the protocols UITableViewDataSource: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/ and UITableViewDelegate: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/

In MainStoryboard.storyboard you can see in the inspector to the right that the TableViewController has a custom class of WTTableViewController

The relevant methods in WTTableViewController.m:

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WeatherCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...


    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
}

Here's some general info about protocols: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

I suggest reading a beginners book or tutorial on iOS to pick up the overall design principles otherwise many things can be quite confusing.

Upvotes: 0

Stefan
Stefan

Reputation: 1335

If you working with TableViewController, UITableViewDelegate and UITableViewDataSource are connected to table automatically. In case you are working with ViewControler which contains TableView, you have to add UITableViewDelegate and UITableViewDataSource like this: @interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>. In new versions of Xcode you can drag delegate and data source to table, and if you want to set delegate programmatically, you can add this two lines of codes:

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


or, equally:

tableview.delegate = self;
tableview.dataSource = self;

Upvotes: 1

Tikhonov Aleksandr
Tikhonov Aleksandr

Reputation: 14329

Because, the basic class adopts from UITableViewController

@interface WTTableViewController : UITableViewController

In your storyboard you just ctrl-drag from tableview to viewController and choose delegate and dataSource.

Upvotes: 1

Mobile Developer
Mobile Developer

Reputation: 5760

Datasource is used to supply data and delegate is used to supply behaviour. UITableView asks your datasource every time it needs data to display. It provides a lot of flexibility for how you choose to represent your underlying data model. You simply define specific methods to use in order to get table information, and iOS can call them when it needs to know something like the number of rows in a section, or the content of a particular row.

You will probably implement your own delegate mechanism in the future. It is a great design pattern which handles interaction/data transfer between objects.

Upvotes: 1

Related Questions