答案 0 :(得分:1)
这就是我的所作所为:
使用以下方法创建委托协议:
- (void)textFieldInCellDidReturn:(UITableViewCell*)cell;
- (void)textFieldInCellDidBeginEditing:(UITableViewCell*)cell;
- (void)textFieldInCellDidEndEditing:(UITableViewCell*)cell;
在单元格中:
-(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;
}
在视图控制器和表视图委托中,声明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];
}
}
答案 1 :(得分:0)
为textField设置顺序标记,例如0,1,2 ..... 导入textField委托方法textFieldDidEndEditing。每当按下返回键时,都会调用此方法。
使用类似的东西:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == currentTextFieldTag+1){
[textField becomesFirstResponder];
}
}
答案 2 :(得分: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