2010-03-05 5 views
62

Comment puis-je obtenir un UITableViewCell par indexPath? Comme ceci:Comment puis-je obtenir un uitableViewCell par indexPath?

J'utilise le UIActionSheet, quand je clique confirme UIButton Je veux changer le texte de cellule.

Le nowIndex Je définirais comme une propriété

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

      } 
     } 
    } 
} 

Répondre

5

Je ne suis pas sûr de ce que votre problème, mais vous devez spécifier le nom du paramètre comme ceci.

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

salut david je réécris la question, pouvez-vous voir à nouveau – phpxiaoxin

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

vous donnera UITableViewCell. Mais je ne suis pas sûr de ce que vous demandez exactement! Parce que vous avez ce code et encore vous demandant comment obtenir uitableviewcell. Quelques informations supplémentaires vous aideront à répondre :)

ADD: Voici une autre syntaxe qui réalise la même chose sans la distribution.

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

* Techniquement * vous voulez appeler sur quel que soit le La propriété 'dataSource' de votre tableView est définie sur, mais généralement' self'. – devios1

26

Enfin, je reçois la cellule en utilisant le code suivant:

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

Parce que la classe est étendue UITableViewController:

@interface SearchHotelViewController : UITableViewController 

Ainsi, le self est "SearchHotelViewController".

+1

Nommer une classe (..) View est source de confusion si c'est une sous-classe de UIViewController. – johnyu

6

Vous pouvez utiliser le code suivant pour obtenir la dernière cellule.

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

Voici un code pour obtenir cellule personnalisé de chemin d'index

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

Pour Swift

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

Dans le cas d'avoir deux cellule personnalisée et j'ai besoin de savoir quel type de cellule sont retournés sur ce chemin d'index. Quelle serait la meilleure approche pour obtenir le type? –

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

Pour Swift 4

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