テーブルビュー セルには 1 つのラベルと 1 つのテキスト フィールドが含まれています。セルを再利用して動作しています。しかし、最初のテキストフィールドにテキストを入力してリターンキーを押すと、次のテキストフィールドに移動したいのですが、どうすればそれを処理できますか?
3 に答える
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]; } }
于 2015-09-10T06:24:08.937 に答える
0
textField の連続タグを設定します (例: 0,1,2.....) textField デリゲート メソッド textFieldDidEndEditing をインポートします。Return キーが押されるたびに、このメソッドが呼び出されます。
次のようなものを使用します。
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == currentTextFieldTag+1){
[textField becomesFirstResponder];
}
}
于 2015-09-10T05:57:54.207 に答える
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];
}
これが役立つことを願っています。
于 2015-09-10T06:20:32.870 に答える