2010-11-03 6 views
0

J'utilise le modèle SplitView de Xcode. J'ai changé le rootviewcontroller pour être un UIViewController et j'ai modifié à la NIB de sorte qu'il a une tableview et quelques autres contrôles. Je souhaite créer un autre contrôle tableView et de recherche dans la NIB. Est-ce possible - pouvez-vous avoir 2 tablesview dans un NIB?2 vues de table dans une plume

Si oui, comment allez-vous différencier les données et déléguer les méthodes?

Répondre

0

Vous pouvez mettre une étiquette différente pour chaque tableView comme ceci:

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

et pour exemple dans cette méthode déléguée:

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

Une autre façon à côté de marquage est d'avoir 2 tableViews IBOutlet marqués Dans votre FirstResponder, connectez-le aux vues de table appropriées dans InfterfaceBuilder.

voir ce code: https://github.com/vikingosegundo/my-programming-examples/tree/master/VSCheckFavorites/

Que les tableviews peuvent être adressées par les membres du contrôleur, qui est connu sous le nom FirstResponder dans le 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, où je montre comment tenir deux tableview sur la vue parent alors que chacun a son propre contrôleur

1
We can have two table views in one NIB. 

par exemple: vous avez 2 vues de table comme


UITableView *tableView1; 
UITableView *tableView2; 

vous pouvez utiliser ci-dessous le code de l'échantillon;


-(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){ 
     ......... 
    } 
} 
Questions connexes