19

Ho un UITabBarController con più di 5 UITabBarItems quindi piùNavigationController è disponibile.Come ottenere il testo della cella basato su indexPath?

Nel mio UITabBarController Delegato faccio la seguente:

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

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

Voglio realizzare un UITableViewDelegate in modo da poter catturare la riga che è stata selezionata, impostare una proprietà visualizzazione personalizzata e quindi spingere il controller della vista:

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

Ho bisogno di ottenere il testo di una cella quando l'utente tocca una riga. Come posso realizzare questo?

risposta

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; 
} 

Una soluzione migliore è quella di mantenere matrice di celle e utilizzarlo direttamente qui

// 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

Perché è meglio mantenere una matrice di cella? – user592419

+0

se si chiama nuovamente cellForRowAtIndexPath solo per acquisire la cella selezionata, non sarà necessario eseguire l'intera funzione e creare nuovamente una cella. Quale sarebbe la prestazione costosa rispetto alla memorizzazione della cella già creata nell'array –

+0

Se si utilizza insertObject, la matrice sarà dappertutto in pochissimo tempo. I documenti dicono "Se l'indice è già occupato, gli oggetti all'indice e oltre vengono spostati aggiungendo 1 ai loro indici per creare spazio.". Quindi, quando cellForRowAtIndexPath viene chiamato una seconda volta (dopo lo scorrimento, forse) viene inserita la stessa cella ma l'originale e tutte le altre celle vengono spinte in avanti nell'array. – amergin

Problemi correlati