19

Tengo un UITabBarController con más de 5 UITabBarItems por lo que el controlador de navegación más está disponible.¿Cómo obtener texto de celda basado en indexPath?

En mi UITabBarController Delegado hago lo siguiente:

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

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

que desea implementar un UITableViewDelegate para que pueda capturar la fila que se ha seleccionado, se fija una vista personalizada propiedad y luego presione el controlador de vista:

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

Necesito obtener el texto de una celda cuando el usuario toca una fila. ¿Cómo puedo lograr esto?

Respuesta

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 mejor solución es mantener la matriz de la célula y utilizarla directamente aquí

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

Por qué es mejor para mantener una serie de la célula? – user592419

+0

si llama de nuevo a cellForRowAtIndexPath para capturar la celda seleccionada, no ejecutará la función completa y creará la celda nuevamente. Lo cual sería un rendimiento costoso en comparación con el almacenamiento de una celda ya creada en el arreglo –

+0

. Si usa insertObject, su matriz estará por todas partes en muy poco tiempo. Los documentos dicen "Si el índice ya está ocupado, los objetos del índice y más allá se desplazan añadiendo 1 a sus índices para hacer espacio". Por lo tanto, cuando se llama a cellForRowAtIndexPath por segunda vez (luego del desplazamiento) se inserta la misma celda, pero la original y todas las otras celdas se empujan hacia adelante en la matriz. – amergin

Cuestiones relacionadas