我认为您的第二个想法是正确的方法。将一个实例变量添加到您的表视图控制器NSMutableArray *_textFields
中,并将其初始化为viewDidLoad
.
然后,在您的tableView:cellForRowAtIndexPath:
方法中,每次添加类似这样的内容:
if ([indexPath row] == 0) {
FDTextFieldCell *cell = [self textFieldCell];
[[cell textLabel] setText:@"Ваше Имя"];
[[cell textField] setPlaceholder:@"Обязательно"];
[[cell textField] setText:[profile name]];
[[cell textField] setReturnKeyType:UIReturnKeyNext];
[[cell textField] setKeyboardType:UIKeyboardTypeDefault];
// ADD THIS
[[cell textField] setTag:[indexPath row]];
if (![_textFields containsObject:[cell textField]]) {
[_textFields addObject:[cell textField]];
[_textFields sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
}
return cell;
}
从那里,您有一个排序的文本字段数组,因此您可以像这样实现您的辅助方法:
- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view {
UITextField *textField = nil;
for (textField in _textFields) {
if ([textField isFirstResponder])
break;
}
NSInteger indexOfFirstResponder = [_textFields indexOfObject:textField];
NSInteger nextIndex = indexOfFirstResponder + 1;
if (nextIndex == [_textFields count])
nextIndex = 0;
UITextField *nextField = [_textFields objectAtIndex:nextIndex];
[nextField becomeFirstResponder];
}
- (void) inputAccessoryViewDidSelectPrev:(FDInputAccessoryView *)view {
UITextField *textField = nil;
for (textField in _textFields) {
if ([textField isFirstResponder])
break;
}
NSInteger indexOfFirstResponder = [_textFields indexOfObject:textField];
NSInteger previousIndex = indexOfFirstResponder - 1;
if (previousIndex < 0)
previousIndex = [_textFields count] - 1;
UITextField *previousField = [_textFields objectAtIndex:previousIndex];
[previousField becomeFirstResponder];
}