0

Is it possible to use one UITableViewController to multiple UIViewControllers. If yes how is it possible, Thanks in advance.

I have 3 screens(UIViewControllers) with same layout those are having UITablewView. So, can I create one UITableViewController for all those screens and can I use that in all the screens(UIViewControllers). If yes please give me some idea and no need to use custom cells

Muddu Patil
  • 713
  • 1
  • 11
  • 24
Vishnu
  • 354
  • 3
  • 19
  • You can use the same `UITableViewController` with different DataSource depending upon the data to be shown. If you want to use different cell, you can also do this in `cellForRowAtIndexPath:` – Akhilrajtr Apr 25 '14 at 06:08

3 Answers3

0

If you simply don't want implement same delegate and data source methods twice, do it in one controller and subclass others from it.

user2260054
  • 522
  • 1
  • 3
  • 11
0

You don't seem to fullly understand what a UITableViewController actually is; it is infact a subclass of UIViewController.

Your UIViewControllers don't need a UITableViewController. If you wish to use UIViewController you should manually implement a UITableView.

Given that a tableView needs a set delegate and datasource, I think it may be a little complicated for you to reuse the same tableView with many ViewControllers. Unless you are happy to oursource this work to a seperate class or hotswap on display.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
0

yes its possible , but you had to add uiitableview in all 3 viewcontrollers .

here is some example..! enter image description here set tag to that tableview as 21

enter image description here

and in tableview implementation file do your implementation like this

@implementation LCsampleTableView

        UITableView *tableView1;
- (id)initWithCoder:(NSCoder *)aDecoder {

    // self = [super init];
    self = [super initWithCoder:aDecoder];

    if (self) {

        tableView1 =(UITableView *)[self viewWithTag:21];
        [tableView1 setDelegate:self];
        [tableView1 setDataSource:self];


        //     [self myviewDidload];

    }

    return self;
}

#pragma mark - Table view start
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;

}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"tblCell"];



    //cell = myCellDeque;
    if(cell == nil){
        //        cell = myCellDeque;

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tblCell"];

        UILabel *lb1 = [[UILabel alloc] initWithFrame:CGRectMake(25, 6, 154, 20)];
        lb1.tag = 1; // Set a constant for this
        lb1.font = [UIFont systemFontOfSize:9.0];
        [lb1 setTextColor:[UIColor whiteColor]];
        lb1.backgroundColor = [UIColor clearColor];

                cell.contentView.backgroundColor= [UIColor colorWithRed:7.0/255.0 green:91.0/255.0 blue:164.0/255.0 alpha:1]  ;

        [cell.contentView addSubview:lb1];


    }


    return  cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {






}



#pragma -mark tableDelete

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {










}



@end

add this tableview with class in all three controllers then you dont had to code for this tableview in all 3 controllers. hope it helps

add tag to the tableview like this enter image description here

ShujatAli
  • 1,376
  • 2
  • 14
  • 26
  • Where is the viewWithTag method and what should I do with viewWithTag method – Vishnu Apr 25 '14 at 06:33
  • note this LCsampleTableView class should be a subclass of uiview than you will get its method viewwithtag – ShujatAli Apr 25 '14 at 06:37
  • I have a situation here after selecting a row I need to navigate to the another viewcontroller – Vishnu Apr 25 '14 at 06:49
  • in tableview did select delegate method you can do performsegue – ShujatAli Apr 25 '14 at 06:51
  • for reference you can view this , http://stackoverflow.com/questions/15478185/performing-segue-from-another-class – ShujatAli Apr 25 '14 at 06:57
  • I'm sorry to say I'm not using the storyboard or nib files – Vishnu Apr 25 '14 at 07:02
  • you can add this view programmatically also . their is no big deal in that. – ShujatAli Apr 25 '14 at 07:21
  • In didSelectMethod I have written this code BBSkillsViewController* skillsViewController = [[BBSkillsViewController alloc] init]; [skillsViewController navigateToOtherViewController] in navigateToOtherViewController code is like this BBViewController* viewController = [[BBViewController alloc] init]; [self.navigationController pushViewController:viewController animated:YES]; but its not working – Vishnu Apr 25 '14 at 07:23
  • problem is this line .....BBViewController* viewController = [[BBViewController alloc] init]; – ShujatAli Apr 25 '14 at 07:28
  • initiate viewcontroller like this ...http://stackoverflow.com/questions/20540125/instantiating-a-view-controller-programmatically-with-storyboard-from-appdelegat – ShujatAli Apr 25 '14 at 07:29