我有一个UITableView
,其中包含cell
和UITextField
的自定义buttons
,问题是当用户选择底部的textfields
时{ {1}}隐藏了keybord
,我试图通过在stackoverflow中查看一些答案来向上滚动textfield
。但它不是滚动任何人都可以帮我找出我所犯的错误。我已经编写了用于UITableView
方法滚动的代码。textFieldDidBeginEditing:
也在触发并执行代码但是它不滚动。
textFieldDidBeginEditing:
答案 0 :(得分:0)
您应该收听键盘通知并更改表格视图框以显示所需的单元格。滚动是不够的,好像单元格位于底部,无论如何都会被隐藏
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
}
-(void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)keyboardWillAppear:(NSNotification *)note
{
CGRect tableViewFrame = self.tableView.frame;
// If your table view doesn't end at the bottom of the screen then this calculation of the height wouldn't be enough, you would need to take into consideration the distance between the screen bottom and the table view
tableViewFrame.size.height = tableViewFrame.size.height - [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect visibleFrame = CGRectZero;
visibleFrame.origin = self.tableView.contentOffset;
visibleFrame.size = tableViewFrame.size;
[UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
animations:^{
self.tableView.frame = tableViewFrame;
}
completion:^(BOOL finished){
[self.tableView scrollRectToVisible:visibleFrame
animated:YES];
}];
}
-(void)keyboardWillDisappear:(NSNotification *)note
{
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = tableViewFrame.size.height + [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
animations:^{
self.tableView.frame = tableViewFrame;
}
completion:nil];
}
答案 1 :(得分:0)
试试这个
[self.myTableView setContentOffset:CGPointZero animated:YES];
OR
// Table View - Scroll to top
[self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.myTableView reloadData];
<强>参数强>
animated:YES
显示滚动动画
滚动后显示animated:NO
显示表(无动画。)
答案 2 :(得分:-2)
在滚动之前必须重新加载tableView。我在textFieldDidBeginEditing中添加了一行代码。请试试这个。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.tabelView1 reloadData];
NSArray *indexPathsForVisibleRows = [self.tabelView1 indexPathsForVisibleRows];
NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count/2)];
[self.tabelView1 scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
祝你好运