[英]UITableView scrollToRowAtIndexPath doesn't scroll to the first row if it only has one item
[英]scrollToRowAtIndexPath doesn't work for first cell
當它開始編輯時,我試圖滾動到表格單元格內的文本字段。 除第一個單元格外,它對其他表單元格都適用:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview;
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
我已經測試了[self.tableView indexPathForCell:cell]的值,該值設置為第一行和第一部分。
我認為這與以下事實有關:第一部分的heightForHeaderInSection非常大(覆蓋大約一半的屏幕)。
我正面臨這個問題,只是通過此博客鏈接解決了
這是過程: 在UITextFieldDelegate的textFieldDidBeginEditing方法中,唯一需要注意的是,在實際顯示鍵盤之前將調用委托。 為了解決這個問題,我們使用延遲的性能來延遲滾動:
-(void) scrollToCell:(NSIndexPath*) path {
[_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:section];
[self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0.5f];
}
我想這是一個錯誤。 通過設置表contentOffset,我可以解決此問題:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint contentOffset = ...; // calculate offset for cell
[UIView animateWithDuration:0.5 animations:^{
self.tableView.contentOffset = contentOffset;
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.