SpacePyro
SpacePyro

Reputation: 3140

Reuse a UITableView that was created programmatically?

I'm working on an app that uses UITableViews with custom, programmatic UI changes. For instance, I have some programmatic UITableViewCell changes:

...
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:14];
cell.selectedBackgroundView = anImageView;
...

And UITableView settings like this:

self.table.separatorColor = [UIColor darkGrayColor];

How would I make a subclass of a UITableView and UITableViewCell to where whenever I create a new table, it would inherit these properties? I have all of these properties in each table (there's about 3-4 of them) and would like to reduce unnecessary code clutter.

Upvotes: 0

Views: 237

Answers (2)

CodaFi
CodaFi

Reputation: 43330

The UITableViewCell would be the easiest to reuse, as they are (should) always be a separate class (and nib if applicable), however, reusing the Table View itself would require a subclass. This is a little much, considering how easy it is to make UITableViews on a per-class basis, but if you feel you must, you can. Be warned, the UITableView class is extremely picky when it comes to subclasses.

Upvotes: 1

thebarcodeproject
thebarcodeproject

Reputation: 565

I'd make two subclasses:

Your own table view subclass (where you could make the changes to the table view in the initWithFrame:style: method) and your own table view cell subclass (where you'd make the changes in the initWithStyle:reuseIdentifier: method).

You may also want to override your table view's dataSource property, and create your own protocol that forces the -(UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path method to return the your UITableViewCell subclass rather than the standard class.

Upvotes: 1

Related Questions