ฉันหวังว่าพวกคุณจะได้รับการแก้ไขอ่านทั้งหมด แต่ฉันพบวิธีแก้ปัญหาของฉันดังนี้ ฉันคาดหวังว่าคุณมีเซลล์UITextFieldอยู่แล้ว ดังนั้นในการเตรียมเก็บดัชนีแถวไว้ในแท็กของฟิลด์ข้อความ
cell.textField.tag = IndexPath.row;
สร้างactiveTextFieldอินสแตนซ์ของUITextFieldด้วยขอบเขตทั่วโลกดังต่อไปนี้:
@interface EditViewController (){
UITextField *activeTextField;
}
ดังนั้นตอนนี้คุณเพียงแค่คัดลอกวางรหัสของฉันในตอนท้าย และอย่าลืมเพิ่มด้วยUITextFieldDelegate
#pragma mark - TextField Delegation
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
activeTextField = textField;
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
activeTextField = nil;
}
ลงทะเบียนคีย์บอร์ด notifications
#pragma mark - Keyboard Activity
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
จับแป้นพิมพ์Notifications:
เรียกว่าเมื่อUIKeyboardDidShowNotificationถูกส่ง
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
[self.tableView setContentInset:contentInsets];
[self.tableView setScrollIndicatorInsets:contentInsets];
NSIndexPath *currentRowIndex = [NSIndexPath indexPathForRow:activeTextField.tag inSection:0];
[self.tableView scrollToRowAtIndexPath:currentRowIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
เรียกว่าเมื่อUIKeyboardWillHideNotificationถูกส่ง
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
[self.tableView setContentInset:contentInsets];
[self.tableView setScrollIndicatorInsets:contentInsets];
}
ตอนนี้มีสิ่งหนึ่งเหลืออยู่เรียกregisterForKeyboardNotificationsใช้ViewDidLoadเมธอดเป็นวิธีดังนี้:
- (void)viewDidLoad {
[super viewDidLoad];
// Registering keyboard notification
[self registerForKeyboardNotifications];
// Your codes here...
}
คุณทำเสร็จแล้วหวังว่าtextFieldsจะไม่ถูกซ่อนโดยคีย์บอร์ดอีกต่อไป