OPが理解しようとしているのは、ユーザーが各フィールドにデータを入力するとUITextField値にアクセスする方法です。これは、@ willcodejavaforfoodによって提案されたようにセルが作成された時点では利用できません。
私はフォームを実装しており、可能な限りユーザーフレンドリーにしようとしています。それは実行可能ですが、あなたが持っているUITableViewCells/UITextFieldの数に応じてかなり混乱することに注意してください。あなたの質問を再にまず
:UITextFieldのの値にアクセスすることは:私も使用
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; // this should return you your current indexPath
// From here on you can (switch) your indexPath.section or indexPath.row
// as appropriate to get the textValue and assign it to a variable, for instance:
if (indexPath.section == kMandatorySection) {
if (indexPath.row == kEmailField) self.emailFieldValue = textField.text;
if (indexPath.row == kPasswordField) self.passwordFieldValue = textField.text;
if (indexPath.row == kPasswordConfirmField) self.passwordConfirmFieldValue = textField.text;
}
else if (indexPath.section == kOptionalSection) {
if (indexPath.row == kFirstNameField) self.firstNameFieldValue = textField.text;
if (indexPath.row == kLastNameField) self.lastNameFieldValue = textField.text;
if (indexPath.row == kPostcodeField) self.postcodeFieldValue = textField.text;
}
}
:
1)あなたのビューコントローラが<UITextFieldDelegate>
2)以下のメソッドを実装してください現在の編集されたフィールドが表示されることを確認するのに似た構文です:
- (void) textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell*) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
そして最後に、あなたは同様にtextViewShouldReturn:
を扱うことができます。
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]];
switch (indexPath.section) {
case kMandatorySection:
{
// I am testing to see if this is NOT the last field of my first section
// If not, find the next UITextField and make it firstResponder if the user
// presses ENTER on the keyboard
if (indexPath.row < kPasswordConfirmField) {
NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:sibling];
[cell.cellTextField becomeFirstResponder];
} else {
// In case this is my last section row, when the user presses ENTER,
// I move the focus to the first row in next section
NSIndexPath *sibling = [NSIndexPath indexPathForRow:kFirstNameField inSection:kOptionalSection];
MemberLoginCell *cell = (MemberLoginCell*)[self.memberTableView cellForRowAtIndexPath:sibling];
[cell.cellTextField becomeFirstResponder];
}
break;
}
...
}
出典
2010-12-07 11:02:18
Rog
乾杯Rog、私は私のMacに戻るときにそれを試してみましょう。私は、Appleがこれをやっているのだろうか、設定アプリのように、私はこれよりもはるかに単純明快だと思っていただろう。私は彼らがおそらく完全に異なったやり方で、コードを使ってやると思います。 – Darthtong