Tables work without using protocols or calling methods?


#1

Hello All,

I noticed in this amazing book that we never conform to <UITableViewDelegate, UITableViewDataSource> that is used in soooooo many examples. Is this because we are subclassing UITableViewController and therefore it isn’t needed, or is there some other reason why?

Second:

I notice in my ItemsViewController.m that I can simply key in…

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {

return [self.listData count];

}

and I never see it getting called anywhere? So if we are just implementing the method where is it getting called to execute? In the UITableViewDataSource protocol?

Just curious it would help fill in the gaps.

Thanks,

iOSNerd.


#2

Yeah, you are right, in that when you subclass a UITableViewController, the dataSource and delegate instance variables of the UITableView are automatically set to point at the UITableViewController (as stated on page 189, and shown in figure 9.4).

With regards to your second question I don’t know what exactly you are asking when you ask “and I never see it getting called anywhere?” - what is “it”?

Nick
http://myfirstiphoneapp.co.uk


#3

Thanks i just saw that in my print copy.

When I say it never gets called by me, I mean no where in the code do i see square brackets calling the above mentioned method. You know what I mean?

Like this is implementation:

-(int)someCoolMethod{
NSLog(@“Actually it isn’t very cool!”);
}

And this would be calling it.

[self someCoolMethod];

Where do these table methods get called if we just put code we want executed in the implementation without ever calling them?


#4

Ah, now I understand your question.

Basically, as your class is a subclass of UITableViewController, the UITableView asks its datasource (your class) certain questions automatically, with

[color=#008000]-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section:[/color]

being one of them.

So when that class is instantiated, this method is called automatically.

If you go to:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

you will see this method, along with [color=#008000]– tableView:cellForRowAtIndexPath:[/color], these methods are “required”.

Hope this helps,
Nick

http://myfirstiphoneapp.co.uk


#5

Awesome, I assumed they had to be called automatically. I just wanted to make sure I fully understand this since the example built a table from scratch. I thought I might as well over analyze it. Thanks… your site is interesting I look forward to reading and catching up on your journey.