gpt4 book ai didi

ios - 转到不同 UITableViewCell 中的下一个 UITextField

转载 作者:行者123 更新时间:2023-11-29 03:41:14 26 4
gpt4 key购买 nike

我有一个包含 x 个单元格的 UITableView 。在某些单元格中,有 x 个 UITextFields。我希望键盘上的下一个按钮突出显示下一个文本字段。我让它在同一个单元格中工作,但当它应该跳转到不同单元格中的文本字段时却不起作用。每个单元格中的第一个文本字段具有标记 0,第二个标记为 1,依此类推。这是我的代码:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

// Fetch current cell
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview;

// Tag for next textField
NSInteger nextTag = textField.tag + 1;

UITextField *nextTextField;

for(UITextField *subview in cell.contentView.subviews){

if([subview isKindOfClass:[UITextField class]]){

if(subview.tag == nextTag){

nextTextField = subview;
break;

}

}

}

if(nextTextField){

// Go to next textField
[nextTextField becomeFirstResponder];

} else{

// IndexPath for current cellen
NSIndexPath *indexPath = [travellersTableView indexPathForCell:cell];

UITableViewCell *nextCell;

// While a cell exist at indexPath
while([travellersTableView numberOfSections] > indexPath.section+1){

nextCell = [self tableView:travellersTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section+1]];

if(nextCell){

for(UITextField *subview in nextCell.contentView.subviews){

if([subview isKindOfClass:[UITextField class]]){

if(subview.tag == 0){

NSLog(@"%@", subview.placeholder);

nextTextField = subview;
break;

}

}

}

// Go to next textField or cell
if(nextTextField){
[nextTextField becomeFirstResponder]; NSLog(@"Next!");
break;
} else{
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section+1];
}

}

}

if(!nextTextField){ [textField resignFirstResponder]; }

}

return NO;

}

NSLog 的行为符合预期。我在文本字段中有不同的占位符,下一个占位符已回显。下一个!也得到回显,因此它试图成为FirstResponder

最佳答案

我发现了这个问题。我没有从我的 tableView 中正确获取 nextCell 。它应该是这样的:

nextCell = [travellersTableView cellForRowAtIndexPath:indexPath];

关于ios - 转到不同 UITableViewCell 中的下一个 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18409413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com