使用时需要设置textField的tag为indexPath.row的值+1
//自动跳转下一个textField
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
//循环寻找TableViewCell
BOOL flag = YES;
UIView * object = textField;
do {
if ([object.superview isKindOfClass:[UITableViewCell class]]) {
flag = NO;
}
object = object.superview;
} while (flag);
//记录textField的父视图有几个TextField以及当前是第几个
int subNumber = 0, indexN = 0;
NSMutableArray *array = [NSMutableArray array];
for (id ob2 in object.subviews[0].subviews) {
if ([ob2 isKindOfClass:[UITextField class]]) {
[array insertObject:ob2 atIndex:0];
if ([NSStringFromCGRect(((UIView *)ob2).frame) isEqualToString:NSStringFromCGRect(textField.frame)]) {
indexN = subNumber;
}
subNumber++;
}
}
//跳转当前父视图的下一个textField
if ((indexN + 1) < subNumber) {
[array[indexN++] becomeFirstResponder];
return YES;
}
//在其他table的cell中寻找textField并跳转
NSInteger maxNumber = [self.tableView numberOfRowsInSection:0];
for (int i = 0;i < maxNumber ; i++) {
//如果textField的tag等于row+1的话取余之前不用加1
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(textField.tag + i) % maxNumber inSection:0]];
for (id ob2 in cell.subviews[0].subviews) {
if ([ob2 isKindOfClass:[UITextField class]]) {
[ob2 becomeFirstResponder];
return YES;
}
}
}
return YES;
}
写的时候也挺费劲。。原创哦。