Overriding UITableViewController's cellForRowAtIndexPath in Objective-C
I have a view controller MainVC which contains a view where I want to switch between various tables. I have an "abstract" class called InfoVC which extends UITableViewController and contains several methods that must be overridden or else they throw an exception. Finally, I have several classes which extend InfoVC and implement the "abstract" methods in InfoVC, along with methods that override tableView's numberOfRowsInSection and cellForRowAtIndexPath.
This is how I load a table in MainVC. In this case, TemperatureInfoVC extends InfoVC, self.subVC is of type InfoVC, and switchView is a view in MainVC:
TemperatureInfoVC *sVC = [[TemperatureInfoVC alloc] initWithStyle:UITableViewStylePlain];
self.subVC = sVC;
[sVC release];
[switchView insertSubview:self.subVC.view atIndex:0];
My problem is that the table always loads empty. From setting breakpoints, I can see that TemperatureInfoVC's numberOfRowsInSection is being called, but neither TemperatureInfoVC's or InfoVC's cellForRowAtIndexPath are being called. However, if I change the first line in the code above to:
InfoVC *sVC = [[InfoVC alloc] initWithStyle:UITableViewStylePlain];
then InfoVC's numberOfRowsInSection and cellForRowAtIndexPath开发者_JAVA百科 are called properly. Does anyone know why TemperatureInfoVC's cellForRowAtIndexPath is not being called?
I assume, that you are not setting the delegate (UITableViewDelegate) and the datasource (UITableViewDatasource). Usually the delegate and the datasource implementation are with-in the custom UITableViewController, so (if not wiring up in IB), there need to be this somewhere:
tableView.delegate = self;
tableView.dataSource = self;
often within the -loadView
or -viewDidLoad:
Oops, figured it out. I was loading data from an NSMutableArray in TemperatureInfoVC which was set in MainVC, but I was setting the data before I initialized subVC, so of course the table had no data in it.
精彩评论