2010-05-24 6 views
1

J'essaye d'ajouter par programme un UITextFiled à l'intérieur d'une de mes cellules de tableview. Comment ferais-je cela?iPhone: UITextField dans la cellule TableView?

En supposant que c'est dans la méthode suivante, quel code utiliser?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 
     cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     // Don't let the user click when not in editing mode 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"Title"; 
      // This is where I want to add a text field 
      cell.detailTextLabel.text = @"test"; 
      cell.editing = YES; 
     break; 
    } 

    return cell; 
} 

Répondre

1

essayez ceci:

[cell.contentView addSubview:[[[UITextField alloc] initWithFrame:CGRectMake(...)] autorelease]]; 
+0

Ok, cela fonctionne. Mais maintenant, comment aurais-je un pointeur vers ce champ de texte, donc je peux faire des choses comme lui donner du texte par défaut, et une prise? –

+0

Assurez-vous que vous pouvez avoir un pointeur vers ce champ de texte. Par exemple, vous pouvez créer un membre de classe pointer, créer une instance UITextField dans le constructeur plutôt que d'ajouter une instance créée dans la cellule. ex: [cell.contentView addSubview: instance]; – Yakov

+0

Ok, j'ai fait ce que vous avez dit, mais le champ de texte n'apparaît pas, j'ai essayé de créer l'instance UITextField et l'assigner à mon membre de classe pointer en utilisant rideTitle = [[UITextField alloc] initWithFrame: CGRectMake (12, 45, 260, 25)]; mais cela ne semble pas fonctionner. Des idées? –

Questions connexes