2010-03-05 10 views
62

Come posso ottenere un UITableViewCell entro il indexPath? Come questo:Come posso ottenere uitableViewCell da indexPath?

Io uso il UIActionSheet, quando faccio clic su conferma UIButton Voglio cambiare il testo della cella.

Il nowIndex definisco come una proprietà

- (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) 
      { 

      } 
     } 
    } 
} 

risposta

5

Io non sono abbastanza sicuro che cosa il vostro problema è, ma è necessario specificare il nome del parametro in questo modo.

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

hi david riscrivo la domanda, si può vedere di nuovo – phpxiaoxin

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

vi darà UITableViewCell. Ma non sono sicuro di cosa esattamente chiedi! Perché hai questo codice e stai ancora chiedendo come ottenere uitableviewcell. Altre informazioni ti aiuteranno a risponderti :)

AGGIUNGI: Ecco una sintassi alternativa che realizza la stessa cosa senza il cast.

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

* Tecnicamente * che si desidera chiamare che in qualunque sia il La proprietà 'dataSource' del tuo tavoloView è impostata su, ma di solito è' self'. – devios1

26

Infine, ho la cella utilizzando il seguente codice:

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

Perché la classe è esteso UITableViewController:

@interface SearchHotelViewController : UITableViewController 

Così, il self è "SearchHotelViewController".

+1

Assegnare un nome a una classe (..) La vista è confusa se è una sottoclasse di UIViewController. – johnyu

6

È possibile utilizzare il seguente codice per ottenere l'ultima cella.

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

Ecco un codice per ottenere cellule personalizzato dal percorso dell'indice

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

Per Swift

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

Nel caso di avere due celle personalizzate e ho bisogno di sapere quale tipo di cella viene restituito su questo percorso dell'indice. Quale sarebbe l'approccio migliore per ottenere il tipo? –

2

Swift 3

012.386.
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

Per Swift 4

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