2010-04-06 11 views
6

Tengo un TableViewController que está usando un estilo agrupado y tiene dos (2) secciones. La primera sección tiene 4 filas y la segunda sección tiene 3 filas. He colocado un UILabel y un UITextField en cada celda, y tengo un método personalizado (textFieldDone :) para manejar el movimiento del cursor al siguiente campo de texto cuando se presiona la tecla de retorno.La tecla de retorno del teclado del iPhone moverá el cursor al siguiente campo de texto

Esto funciona bien y dandy si sólo hay una sección, pero tengo dos :(y sí necesito dos :)

así que empecé Codin' inventar una respuesta, pero no obtuvo los resultados que simplemente no lo hacen trabajo, me di cuenta durante mi depuración que el identificador de la celda (yo uso dos) solo muestra uno (en la consola de depuración) y es el primero (celda genérica).

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 

    switch (indexPath.section) 
    { 
     case AUTO_DETAILS: 
     { 
      static NSString *cellID = @"GenericCell"; 

      cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
               reuseIdentifier:cellID] autorelease]; 

       UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 25)]; 
       label.tag   = kLabelTag; 
       label.font   = [UIFont boldSystemFontOfSize:14]; 
       label.textAlignment = UITextAlignmentRight; 
       [cell.contentView addSubview:label]; 
       [label release]; 


       UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 200, 25)]; 
       textField.clearsOnBeginEditing = NO; 
       [textField setDelegate:self]; 
       [textField addTarget:self action:@selector(topTextFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
       [cell.contentView addSubview:textField]; 
      } 

      NSInteger row   = [indexPath row]; 
      UILabel  *label  = (UILabel *)[cell viewWithTag:kLabelTag]; 
      UITextField *textField = nil; 

      for (UIView *oneView in cell.contentView.subviews) 
      { 
       if ([oneView isMemberOfClass:[UITextField class]]) 
        textField = (UITextField *)oneView; 
      } 

      label.text = [topCellLabels objectAtIndex:row]; 
      NSNumber *rowAsNum = [[NSNumber alloc] initWithInt:row]; 


      switch (row) 
      { 
       case kMakeRowIndex: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.make; 
        break; 
       case kModelRowIndex: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.model; 
        break; 
       case kYearRowIndex: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.year; 
        break; 
       case kNotesRowIndex: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.notes; 
        break; 
       default: 
        break; 
      } 

      if (textFieldBeingEdited == textField) 
      { 
       textFieldBeingEdited = nil; 
      } 

      textField.tag = row; 
      [rowAsNum release]; 
      break; 
     } 
     case AUTO_REGISTRATION: 
     { 

      static NSString *AutoEditCellID = @"AutoEditCellID"; 

      cell = [tableView dequeueReusableCellWithIdentifier:AutoEditCellID]; 

      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
               reuseIdentifier:AutoEditCellID] autorelease]; 

       UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 25)]; 
       label.tag   = kLabelTag; 
       label.font   = [UIFont boldSystemFontOfSize:14]; 
       label.textAlignment = UITextAlignmentRight; 
       [cell.contentView addSubview:label]; 
       [label release]; 


       UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 200, 25)]; 
       textField.clearsOnBeginEditing = NO; 
       [textField setDelegate:self]; 
       [textField addTarget:self action:@selector(bottomTextFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
       [cell.contentView addSubview:textField]; 
      } 


      NSInteger row   = [indexPath row]; 
      UILabel  *label  = (UILabel *)[cell viewWithTag:kLabelTag]; 
      UITextField *textField = nil; 

      for (UIView *oneView in cell.contentView.subviews) 
      { 
       if ([oneView isMemberOfClass:[UITextField class]]) 
        textField = (UITextField *)oneView; 
      } 

      label.text = [bottomCellLabels objectAtIndex:row]; 
      NSNumber *rowAsNum = [[NSNumber alloc] initWithInt:row]; 

      switch (row) 
      { 
       case 0: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.vinNumber; 
        break; 
       case 1: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.policyNumber; 
        break; 
       case 2: 
        if ([[tempValues allKeys] containsObject:rowAsNum]) 
         textField.text = [tempValues objectForKey:rowAsNum]; 
        else 
         textField.text = automobile.licensePlate; 
        break; 
       default: 
        break; 
      } 

      if (textFieldBeingEdited == textField) 
      { 
       textFieldBeingEdited = nil; 
      } 

      textField.tag = row; 
      [rowAsNum release]; 
      break; 
     } 
     default: 
      break; 
    } 
    return cell; 
} 

Ahora recuerde que la primera sección está funcionando muy bien y el código de ese método es el siguiente:

-(IBAction)topTextFieldDone:(id)sender 
{ 
    UITableViewCell *cell    = (UITableViewCell *)[[sender superview] superview]; 
    UITableView  *table    = (UITableView *)[cell superview]; 
    NSIndexPath  *textFieldIndexPath = [table indexPathForCell:cell]; 

    NSUInteger row = [textFieldIndexPath row]; 
    row++; 

    if (row > kNumOfEditableRows) 
     row = 0; 

    NSUInteger newIndex[] = {0, row}; 
    NSIndexPath  *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2]; 
    UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:newPath]; 
    UITextField  *nextField = nil; 

    for (UIView *oneView in nextCell.contentView.subviews) 
    { 
     if ([oneView isMemberOfClass:[UITextField class]]) 
      nextField = (UITextField *)oneView; 
    } 
    [nextField becomeFirstResponder]; 

} 

Fue mi idea de crear sólo un segundo método (secondSectionTextFieldDone :) como esto

-(IBAction)bottomTextFieldDone:(id)sender 
{ 
    UITableViewCell *cell    = (UITableViewCell *)[[sender superview] superview]; 
    UITableView  *table    = (UITableView *)[cell superview]; 
    NSIndexPath  *textFieldIndexPath = [table indexPathForCell:cell]; 

    NSUInteger row = [textFieldIndexPath row]; 
    row++; 

    if (row > 3) 
     row = 0; 

    NSUInteger newIndex[] = {0, row}; 
    NSIndexPath  *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2]; 
    UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:newPath]; 
    UITextField  *nextField = nil; 

    NSString *string = [NSString stringWithFormat:@"AutoEditCellID"]; 
    for (UIView *oneView in nextCell.contentView.subviews) 
    { 
     NSLog(@"%@", nextCell.reuseIdentifier); /* DEBUG LOG */ 
     if ([oneView isMemberOfClass:[UITextField class]] && (nextCell.reuseIdentifier == string)) 
      nextField = (UITextField *)oneView; 
    } 

    [nextField becomeFirstResponder]; 

} 

pero el resultado no resuelve el problema.

así que mi pregunta es, ¿cómo puedo hacer que el cursor salte al siguiente campo de texto en la sección en la que está, si hay uno, y si no, envíe un mensaje "resignFirstResponder" para que el teclado se va.

Respuesta

31

aplicar UITextFieldDelegate

yourFirstTextFeld.delegate = self; 
yourSecondTextFeld.delegate=self; 

poner en práctica este método

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
if(theTextField==yourFirstTextFeld){ 
    [yourSecondTextFeld becomeFirstResponder]; 
} 
return YES; 
} 
Cuestiones relacionadas