Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating UITableView's delegate/datasource from main ViewController

I'm quite new to iOS development. I have read about the issues with ViewControllers becoming huge and would like to follow the design I have seen in the answer to this previous question UITableView issue when using separate delegate/dataSource for my app which has 2 different tables and couple of buttons on one screen. But somehow I get confused in the storyboard connections to make between TestTableViewController and TestTableTestViewController.

Can anyone provide a sample working project or some screen shots on how to connect the UITableView delegate, data source and connecting outlet to the separate custom UIViewController subclass (TestTableTestViewController) in storyboard please?

Also, does this design work with xCode 5 / iOS 7 and above?

Note: For those having moved to Swift I strongly recommend using swift extensions for the delegate & data source and in fact any other implementation of an inherited class or protocol as per the 'Grouping' section of Natasha The Robot's blog post here

like image 322
Litome Avatar asked Oct 01 '22 11:10

Litome


2 Answers

You have to create another class named as MyTableViewDataSource and implement TabbleViewDataSource methods in it. Create an a property of your data source class. Set data source in ViewController Here is the examle:

@interface MyTableViewDataSource ()
@property (nonatomic, assign) int sectionOneRows;
@property (weak) id<YourDelegate> delegate;
@property (nonatomic, strong) NSArray *dataSourceArray;
@end

@implementation MyTableViewDataSource
@synthesize delegate=_delegate;

-(id) initDataSourceWithdArray:(NSArray *)array
                                            delegate:(id)delegate
{
    if (self = [super init]) {
        self.dataSourceArray=array;
        self.delegate = delegate;
    }
    return self;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cellToReturn = nil;
    //Your code here
    return cellToReturn
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //Your logic here
    //implement the delegate method in you ViewController
        [self.delegate performSelector:@selector(loadDetailsAtIndexPath:)withObject:indexPath];
    }
}

In your ViewController:

self.myDataSource = [[MyTableViewDataSource alloc] initDataSourceWithdArray:self.dataArray delegate:self];
[self.myTableView setDataSource:self.myDataSource];
[self.myTableView setDelegate:self.myDataSource];
[self.myTableView reloadData];

Your delegate method:

-(void)loadDetailsAtIndexPath:(NSIndexPath *)indexPath
{
    MyDetailViewController *myDetailController = [[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //Your logic here
    [self.navigationController pushViewController:myDetailController animated:YES];
}

Go to the Connections Inspectors and make changes like this:

enter image description here

Data source for the tableView will be set programatically here rather in IB. I hope this will help you.

like image 64
iBug Avatar answered Oct 04 '22 21:10

iBug


Press and hold Ctrl and click + hold + drag on item you want to make outlet to .h file. it will make connection by itself you just have to name them. See This Video Tutorial

Also check these tutorials

Link 1

Link 2

like image 30
iAhmed Avatar answered Oct 04 '22 21:10

iAhmed