What I want is to centralize all my delegate methods into a class. Where I can either use its defaults or override the delegate method.
For example:
ViewController.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@end
ViewController.h
#import "ViewController.h"
#import "TableDelegateContainers.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
TableDelegateContainers.h
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return sample.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return cell;
}
Basically all the delegate methods that the UITableViewDataSource need will be put to another class. And when I import that class to thew ViewController.h those methods will be used. And I can either overwrite them if I want to.
Is this achievable by class? Or other entity is needed?
Yes, you can have it like this.
You can also do the same in the Xib too.
I think he wants to extract the delegate methods into a separate class. so create a custom class:
TableViewHelper.h
TableViewHelper.m
ViewController.m
so your ViewController does not need to implement both protocols anymore.
Create a CustomViewController (A UIViewController Subclass). Your custom view controller should confirm to all the protocols, provide datasource and handle delegates. After that when ever you create any UIViewController class, just inherit from your own CustomViewController, and override all the methods you need.
You may also keep an IBOutlet of UITableView, and connect with your child controllers where its needed. This way you will have many other benefits like you can keep activity indicator methods in common place, and handle other local notifications for common use.
For the documentation:
So basically you can create a class (a
ViewController
class for example) that conforms to the protocols, and you can make a specificInheritedViewController
inherit from the other class that implements the protocolsUse protocol for this
here i have added an user friendly link hope you get something relevant to you