Let’s say I have 10 tableViews that all need to have the first cell’s background , in all sections , color set to red.
In order not to do that manually for all the 10 table views I’m thinking that I should subclass UITableView.
My question would be: what should I overwrite from UITableView?
Or should I subclass UITableViewCell and all cells inherit from here?
Thanks.
You can subclass UITableViewCell and then you will have to somehow give the section&row to the cell so that it knows to set the background.
However, you can also subclass UITableView. Below is a solution if you choose to subclass UITableView, but in the end it’s your decision.
The only way I can think of right now is to somehow catch all requests to the tableView data source so that you can then manipulate the results.
You can do it like this:
Subclass UITableView, let’s call it MyUITableView like this:
Add a member variable
id<UITableViewDataSource> myDataSource, and also a property for this variable with IBOutlet set to it. Then in Interface Builder, you should connect the table view using this property instead of the standard uitableview datasource property.Somewhere in the
initorloadView, writeself.dataSource = self. The idea is to catch all requests (expeciallycellForRowAtIndexPath) so that you can manipulate the actual results.In your subclass, implement the UITableViewDataSource protocol, and just forward all calls to the myDataSource object.
The only exception is in the
cellForRowAtIndexPathimplementation where after you get the result from myDataSource, you can change the background color if a specific condition is met. See an example code below@interface MyUITableView : UITableView {
id<UITableViewDataSource> myDataSource;
}
@property (nonatomic, retain) IBOutlet id<UITableViewDataSource> myDataSource;
@end
And this is pretty much all you need. I don't know if I was clear enough, but if you have any questions don't hesitate.