简体   繁体   中英

Subclassing a UIViewController with a UITableView

I'm trying to subclass a UIViewController that has an embedded UITableView and is a UiTableViewDataSource and UITableViewDelegate.

The master class looks like this:

@interface PFUIViewControllerWithTable : PFUIViewController     <UITableViewDelegate,UITableViewDataSource, RKObjectLoaderDelegate> {
UITableView *_tableView;
NSArray *_data;
}
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSArray *data;
- (void)configureCell:(PFRewardsUITableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath;

and the implementation contains methods for populating the table view from the self.data property.

In the subclass, the data is populated and [self.tableView refresh] is called

@interface MySubclass : PFUIViewControllerWithTable <UITableViewDataSource,     UITableViewDelegate> {

}
- (void)loadObjectsFromDataStore;
- (void)loadLiveData;
- (void)configureCell:(PFRewardsUITableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath;

The UItableViewDataSource methods are never called in this configuration. The delegate and data source are set to the master class. The master class populates the tableview from the self.data property, which is modified by the subclass.

So, what is the way to subclass such a view?

Use IBOulet for tableview

Now check in xib of your view controller that table view is binded and its delegate and datasource provided to file owner.

Now check array you are providing to tableview has content in it

Make sure that you're properly setting your delegate and datasources.

This is a mistake:

PFUIViewControllerWithTable *controller = [[MySubclass alloc] init];
[controller.tableView setDelegate:controller];
[controller.tableView setDataSource:controller];

This should be ok:

PFUIViewControllerWithTable *controller = [[MySubclass alloc] init];
[controller.tableView setDelegate:(MySubclass*)controller];
[controller.tableView setDataSource:(MySubclass*)controller];

I belive there's where your problem lies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM