开发者

Confused over UITableView numberOfRowsInSection syntax

开发者 https://www.devze.com 2023-02-23 13:23 出处:网络
I have a tab view that loads a table view for each of the tabs First tab interface declares UITableView *tableView;

I have a tab view that loads a table view for each of the tabs

First tab interface declares UITableView *tableView; Second tab interface declares UITableView *favTableView;

When declaring the number of rows for the second table this works:

- (NSInteger)tableView:(UITableView *)favTableView numberOfRowsInSection:(NSInteger)section {
    return [favList count];
}

But if I change it to:

- (NS开发者_StackOverflow社区Integer)favTableView:(UITableView *)favTableView numberOfRowsInSection:(NSInteger)section {
    return [favList count];
}

the app crashes when I try to load the second tab

Is my mistake (a) not understanding which is a variable/reserved word, (b) giving each table a unique identifier ie favTableView, instead of reusing tableView.

Also the second table doesn't have a title bar


The delegate method you must implement is

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

In this method prototype, tableView: (note the colon) is a fixed name you cannot change. The second instance of tableView is simply a local variable name that has meaning within the method. The following would also be valid:

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

The delegate method names are what they are. You cannot decide that you want the delegate method names to be something else, or how would UITableView know what methods to call when it needed information from its delegate?

So, for your table favTableView, if you specified the object that implements the above delegate method as favTableView's delegate, then when called the local variable tv would in fact be the same as favTableView.

I can see where you'd be confused about this. The SDK uses 'tableView' for a lot of things: method prototype placeholder names, variable names, and who knows what else. It boils down to being able to read and understand Objective-C method signatures. :-) It's a little strange, until you get used to it.


The delegate probably uses pre-set method names, as you point out. There shouldn't be a functional problem with not reusing tableView, as you probably reuses favTableView anyway. The title bar-thing should be solved by manually setting the properties for the title in your custom tableView. If you are looking for headers, you have to set the properties for height and size.

0

精彩评论

暂无评论...
验证码 换一张
取 消