EDIT: Does anyone have useful links on this topic? I mean good methods for writing reusable code and "abstraction"?
tl; dr - Read this comment Testing a UIViewController like Cocoa ones
I have 3 UITableViewController
:
CategoriesViewController
RecipesViewController
IngredientsViewController
They are ordered hierarchically. The following is an example hierarchy:
- Dessert (category)
- Brownie (recipe)
- Milk (ingredient)
- Chocolate (ingredient)
- Oil (ingredient)

Each of them has similar functionality with the others. For example, everyone has sorting (moving lines), deleting, adding (representing a modal representation), etc.
Currently, I have repeated all the code for each view controller, customizing the parts associated with each. For example, they all have an instance variable like this:
CategoriesViewController.m
:
@implementation CategoriesViewController { NSMutableArray *categories; }
RecipesViewController.m
:
@implementation RecipesViewController { NSMutableArray *recipes; }
IngredientsViewController.m
:
@implementation IngredientsViewController { NSMutableArray *ingredients; }
Since I think there is a better way to organize this view controller, I tried to create the skeleton MyListViewController.h
:
@interface MyListViewController : UITableViewController @property (nonatomic, strong) NSMutableArray *list; @end
MyListViewController.m
:
@implementation MyListViewController - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_list count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListCell"]; id currentObject = [_list objectAtIndex:indexPath.row]; cell.textLabel.text = [currentObject valueForKey:@"name"]; return cell; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) {
Thus, once I have subclassed it, I need to assign list
ivar to my data structure. And I can even override methods like didFinishDeletingItem:
to customize the behavior of each controller.
Since this is the first time I will use the best practices for writing and organizing code in this way, I would like to know your opinions and which are the best ways for abstract classes to reuse them correctly with the DRY principle.