Deploy delegate at run time?

I have a generic view controller class that inherits all view controller classes in my application that has the following loadView method:

 - (void) loadView { if (viewType == UIStandardViewControllerViewTypeUIView) { UIView *view = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]]; [self setView: view]; [view release]; } else if (viewType == UIStandardViewControllerViewTypeUIScrollView) { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]]; [self setView: scrollView]; [scrollView release]; } else if (viewType == UIStandardViewControllerViewTypeUITableViewPlain || viewType == UIStandardViewControllerViewTypeUITableViewGrouped) { UITableViewStyle tableViewStyle; if (viewType == UIStandardViewControllerViewTypeUITableViewPlain) { tableViewStyle = UITableViewStylePlain; } else { tableViewStyle = UITableViewStyleGrouped; } UITableView *tableView = [[UITableView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame] style: tableViewStyle]; [self setView: tableView]; [tableView release]; } UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; [[self navigationItem] setBackBarButtonItem: backButton]; [backButton release]; } 

I did it this way for many reasons that I don’t feel like getting in. In any case, as you noticed, one of the types of views for implementation is tableview . As we all know, a tableview needs a delegate and a datasource . I was wondering if it is possible to implement <UITableViewDelegate, UITableViewDataSource> at runtime, when I know that tableView is the selection that has been made?

If not, does anyone have any ideas how I can do this without having to manually implement my delegate and data source in the class class of the inherited class? If I implement a data source and delegate at compile time (usually) in the UIStandardViewController class, then I get a warning because I need to implement a required data source and delegate methods in my class of the standard view class. Could you implement them and then just override them in the child class? Or does anyone know how I can do this cleanly?

UPDATE: I wonder if I just implemented the delegate and data source in my UIStandardViewController class and also implemented empty versions of the required methods, was there a lot of extra overhead when I didn't use tableview?

+1
source share
1 answer

You can write a controller (only a controller, not a view controller) that implements a data source and a table view delegate. If necessary, you will create an instance.

Also note that you are using the Factory pattern. You must use the class method to create new views. The signature will look like +(UIView *)viewWithType:(ViewTypeStyle) viewTypeSyle)

TableController.h

 #import <Foundation/Foundation.h> @interface TableController : NSObject <UITableViewDataSource,UITableViewDelegate> @end 

TableController.m

 #import "TableController.h" @implementation TableController -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 100; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; } cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row]; return cell; } @end 

ViewController.m

 #import "ViewController.h" #import "TableController.h" @interface ViewController () @property(nonatomic,retain) UITableView *tableView; @property(nonatomic,retain) TableController *controller; @end @implementation ViewController @synthesize tableView = tableView_; @synthesize controller = controller_; -(void)dealloc { self.tableView = nil; self.controller= nil; [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.controller = [[[TableController alloc] init] autorelease]; self.tableView = [[[UITableView alloc] initWithFrame:self.view.frame] autorelease]; self.tableView.delegate = self.controller; self.tableView.dataSource= self.controller; [self.view addSubview:self.tableView]; // Do any additional setup after loading the view, typically from a nib. } //... @end 
+3
source

Source: https://habr.com/ru/post/1394794/


All Articles