19

J'ai un UITabBarController avec plus de 5 UITabBarItems donc plusNavigationController est disponible.Comment obtenir du texte de cellule basé sur indexPath?

Dans mon délégué UITabBarController je fais ce qui suit:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
//do some stuff 
//... 

UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view; 
    moreView.delegate = self; 
} 

Je veux mettre en œuvre un UITableViewDelegate je peux saisir la ligne qui a été sélectionné, définissez une vue sur la propriété personnalisée, puis appuyez sur le contrôleur de vue:

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //how can I get the text of the cell here? 
} 

Je dois obtenir le texte d'une cellule lorsque l'utilisateur appuie sur une ligne. Comment puis-je accomplir cela?

Répondre

52
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     //how can I get the text of the cell here? 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     NSString *str = cell.textLabel.text; 
} 

Une meilleure solution est de maintenir Tableau de cellule et de l'utiliser directement ici

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    Service *service = [self.nearMeArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = service.name; 
    cell.detailTextLabel.text = service.description; 
    if(![self.mutArray containsObject:cell]) 
      [self.mutArray insertObject:cell atIndex:indexPath.row]; 
    return cell; 
} 



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

    UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row]; 
    NSString *str = cell.textLabel.text; 

} 
+3

Pourquoi est-il préférable de maintenir un tableau de la cellule? – user592419

+0

si vous appelez cellForRowAtIndexPath à nouveau juste pour capturer la cellule sélectionnée, il sera inutile d'exécuter la fonction entière et de créer à nouveau la cellule. Ce qui serait une performance coûteuse comparer à stocker la cellule déjà créée dans le tableau –

+0

Si vous utilisez insertObject votre tableau sera partout dans le temps. Les docs disent "Si l'index est déjà occupé, les objets à l'index et au-delà sont décalés en ajoutant 1 à leurs index pour faire de la place.". Ainsi, lorsque cellForRowAtIndexPath est appelée une seconde fois (après le défilement), la même cellule est insérée mais l'original et toutes les autres cellules sont poussés vers l'avant dans le tableau. – amergin

Questions connexes