开发者

2 tableviews in a nib

I am using Xcode's SplitView template. I have changed the rootviewcontroller to be a UIViewController and I have modified to the NIB so that it has a tableview and a few other controls.

I want to create another tableview and search control in the NIB. Is this possible - can you have 2 tableview's in one NIB?

If 开发者_如何转开发yes, how will you differentiate the data and delegate methods?


Another way beside tagging is to have 2 IBOutlet-marked tableViews in your FirstResponder adn connect it to the appropriate tableviews in InfterfaceBuilder.

see this code: https://github.com/vikingosegundo/my-programming-examples/tree/master/VSCheckFavorites/

Than the tableviews can be addressed by the members of the Controller, that is known as FirstResponder in the Nib

- (void)viewDidLoad {
    self.showTableController = [[ShowFavoritesTableController alloc] init];
    self.checkTableController= [[CheckTableController alloc] init];

    showTable.delegate = self.showTableController;
    showTable.dataSource=self.showTableController;

    checkTable.delegate = self.checkTableController;
    checkTable.dataSource=self.checkTableController;

    self.showTableController.tableView = showTable;
    self.checkTableController.tableView = checkTable;


    [super viewDidLoad];

}

Here I published a sample code, where I show how to hold two Tableview on parent view while each has it own controller


We can have two table views in one NIB.

eg: you are having 2 table views as


UITableView *tableView1;
UITableView *tableView2;

you can use below sample code;


-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    if(tableView == tableView1)
        return 1;
    else if(tableView == tableView2)
        return 2;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(tableView == tableView1)
        return @"Table View 1";
    else if(tableView == tableView2){
        if(section == 1)
        return @"section 1 in table view 2";
    else
        return @"section 2 in table view 2";
    }
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
    if(tableView == tableView1)
        return 5;
    else if(tableView == tableView2){
    if(section == 0)
        return 3;
    else 
        return 4;
    }
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == tableView1){
        .........
    }
    else if(tableView == tableView2){
        .........
    }
}


You can put a different tag for each tableView like this :

tabView1.tag = 100;
tabView2.tag = 200;

and for exemple in this delegate method :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView.tag==100)
    {
        // Return height of the first tabView
    }
    else
    {
        // Return height of the second tabView
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜