2015-09-10 4 views
0

В моей ячейке tableview есть одна метка и одно текстовое поле, я повторно использую свою ячейку, и она работает. но когда я ввожу текст в первом текстовом поле и нажимаю return, я хочу перейти к следующему текстовому полю, как я могу это обработать?Как обращаться с ячейкой Table View с текстовым полем

enter image description here

+1

сделать следующее текстовое поле в качестве первого ответчика после возвращения ключа печати первой TextField –

+0

иого это http://stackoverflow.com/questions/4375442/accessing-uitextfield-in-a-custom-uitableviewcell –

ответ

0

Установить последовательный тег для TextField, например, 0,1,2 ..... Импортировать текстовый метод делегата textFieldDidEndEditing. Всякий раз, когда нажата клавиша возврата, этот метод вызывается.

использовать что-то вроде этого:

-(void)textFieldDidEndEditing:(UITextField *)textField{ 
    if(textField.tag == currentTextFieldTag+1){ 
    [textField becomesFirstResponder]; 
} 
} 
0

в камере для использования функции строки этот код

cell.textField.tag = indexPath.row; 

Теперь реализовать UITextFieldDelegate метод

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag+1 inSection:YOUR_SECTION]; 

UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath]; 
[cell.textField becomesFirstResponder]; 


// if you need [yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 

Я надеюсь, что это поможет. Thankyou

1

Вот что я сделал:

  1. создал протокол делегата с этими методами:

    - (void)textFieldInCellDidReturn:(UITableViewCell*)cell; 
    - (void)textFieldInCellDidBeginEditing:(UITableViewCell*)cell; 
    - (void)textFieldInCellDidEndEditing:(UITableViewCell*)cell; 
    
  2. В клетке:

    -(BOOL)becomeFirstResponder 
    { 
        return [self.inputTextField becomeFirstResponder]; 
    } 
    
    -(void)textFieldDidBeginEditing:(UITextField *)textField 
    { 
        if ([self.delegate respondsToSelector:@selector(textFieldInCellDidBeginEditing:)]) 
        { 
         [self.delegate textFieldInCellDidBeginEditing:self]; 
        } 
    } 
    -(void)textFieldDidEndEditing:(UITextField *)textField 
    { 
        if ([self.delegate respondsToSelector:@selector(textFieldInCellDidEndEditing:)]) 
        { 
         [self.delegate textFieldInCellDidEndEditing:self]; 
        } 
    } 
    -(BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
        if ([self.delegate respondsToSelector:@selector(textFieldInCellDidReturn:)]) 
        { 
         [self.delegate textFieldInCellDidReturn:self]; 
        } 
        return NO; 
    } 
    
  3. В вид контроллера и вид таблицы делегата, объявить iVar NSIndexPath* indexPathOfFirstResponder и принять делегата

    - (void)textFieldInCellDidBeginEditing:(UITableViewCell *)cell 
    { 
        indexPathOfFirstResponder = [self.theTable indexPathForCell:cell]; 
    } 
    
    -(void)textFieldInCellDidReturn:(UITableViewCell *)cell 
    { 
        NSIndexPath* indexPathForCell = [self.theTable indexPathForCell:cell]; 
        if(indexPathForCell.row < [self.theTable numberOfRowsInSection:indexPathForCell.section]-1) 
        { 
         NSIndexPath* nextIndexPathInSection = [NSIndexPath indexPathForRow:indexPathForCell.row+1 inSection:indexPathForCell.section]; 
         UITableViewCell* nextCellInSection = [self.theTable cellForRowAtIndexPath:nextIndexPathInSection]; 
         if (nextCellInSection) 
         { 
          [nextCellInSection becomeFirstResponder]; 
         } 
         else 
         { 
          indexPathOfFirstResponder = nextIndexPathInSection; 
          [self.theTable scrollToRowAtIndexPath:nextIndexPathInSection atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES]; 
         } 
        } 
        else 
        { 
         [self.theTable endEditing:YES]; 
        } 
    } 
    
    -(void)textFieldInCellDidEndEditing:(UITableViewCell *)cell 
    { 
        indexPathOfFirstResponder = nil; 
    } 
    
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        if ([indexPath isEqual:indexPathOfFirstResponder]) 
        { 
         [cell becomeFirstResponder]; 
        } 
    }