2010-03-05 18 views
62

¿Cómo puedo obtener un UITableViewCell por indexPath? De esta manera:¿Cómo puedo obtener uitableViewCell por indexPath?

Uso el UIActionSheet, cuando hago clic en confirmar UIButton Quiero cambiar el texto de la celda.

El nowIndex defino como una propiedad

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     NSInteger section = [nowIndex section]; 
     NSInteger row = [nowIndex row]; 
     NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
     formatter.dateFormat = @"yyyy-MM-dd"; 

     if (section == 0) 
     { 
      if (row == 0) 
      { 

      } 
      else if (row == 1) 
      { 
       UILabel *content = (UILabel *)[[(UITableView *)[actionSheet ] cellForRowAtIndexPath:nowIndex] viewWithTag:contentTag]; 
       content.text = [formatter stringFromDate:checkInDate.date]; 
      } 
      else if (row == 2) 
      { 

      } 
     } 
    } 
} 

Respuesta

5

No estoy muy seguro de cuál es su problema, pero es necesario especificar el nombre del parámetro como tal.

-(void) changeCellText:(NSIndexPath *) nowIndex{ 
    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag]; 
    content.text = [formatter stringFromDate:checkInDate.date]; 
} 
+0

Hola David i reescribir la pregunta, ¿puedes verlo de nuevo – phpxiaoxin

111
[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] 

te dará UITableViewCell. ¡Pero no estoy seguro de qué es exactamente lo que estás pidiendo! Porque tienes este código y sigues preguntando cómo obtener uitableviewcell. Más información ayudará a responderte :)

AÑADIR: Aquí hay una sintaxis alternativa que logra lo mismo sin el reparto.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex]; 
+1

* Técnicamente * al que desea llamar que en cualquiera que sea el La propiedad 'dataSource' de su tableView está configurada a, pero generalmente eso es' self'. – devios1

26

Por último, me sale la célula usando el siguiente código:

UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex]; 

Debido a que la clase se extiende UITableViewController:

@interface SearchHotelViewController : UITableViewController 

Así, el self es "SearchHotelViewController".

+1

Nombrar una clase (..) La vista es confusa si se trata de una subclase de UIViewController. – johnyu

6

Puede usar el siguiente código para obtener la última celda.

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath]; 
14

Aquí hay un código para obtener células personalizado de ruta del índice

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; 
YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath]; 

Para Swift

let indexpath = NSIndexPath(forRow: 2, inSection: 0) 
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest 
+0

En el caso de tener dos celdas personalizadas, necesito saber qué tipo de celda se devuelve en esta ruta de índice. ¿Cuál sería el mejor enfoque para obtener el tipo? –

2

Swift 3

let indexpath = NSIndexPath(row: 0, section: 0) 
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)! 
3

Swift

let indexpath = IndexPath(row: 0, section: 0) 
if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> { 
    cell.backgroundColor = UIColor.red 
} 
4

Para Swift 4

let indexPath = IndexPath(row: 0, section: 0) 
    let cell = tableView.cellForRow(at: indexPath) 
Cuestiones relacionadas