Reputation: 1990
I am writing a custom subclass of UITableView. I would need this object itself to be its own data source and delegate, and this subclass would then have its own data source and delegate. This is done primarily so I can intercept calls to the datasource and delegate and potentially augment them before sending them off to their actual datasources.
My class is defined as so.
CustomTableView : UITableView<UITableViewDelegate, UITableViewDataSource> {
...
id customDataSource;
id customDelegate;
}
The problem comes when I try to set my data source and delegate.
I would like to override uitableview's properties:
- (void)setDataSource(id<UITableViewDataSource>)ds {
[super setDataSource:self]
customDataSource = ds;
}
Basically, I would like to tell the parent class(UItableView) to set the data source to self. I would then forward any callbacks to the customDataSource, after I have modified them.
[super setDataSource:self] doesnt crash, but the datasource never gets set. Does anyone have any ideas? Thanks
Upvotes: 2
Views: 1398
Reputation: 1990
I ended up not needing to use the method proposed in this question, but I did get it working. The problem was that I had accidentaly synthesized the properties that needed overriding, namely dataSource and delegate.
For people who need to do this in the future, simply override setDelegate and setDataSource in your custom subclass.
Upvotes: 2
Reputation: 39502
Dont assign the datasource to self. Create an intermediate object, which you contain in your CustomTableView, and set the datasource to that. Call it DataSourceInterceptor or something.
Another way to accomplish this would be to method-swizzle the datasource object that is being set.
Upvotes: 0