UITableView scrollToRowAtIndexPath不工作的最后4 [英] UITableView scrollToRowAtIndexPath not working for last 4

查看:214
本文介绍了UITableView scrollToRowAtIndexPath不工作的最后4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个UITableViewController和我有单元格内的textfields。当用户点击文本字段,我实现UITextFieldDelegate和在方法textFieldDidBeganEditing我确定单元格的索引,滚动到该位置。它适用于所有的电池,期望最后4-5个单元格。我做错了什么?感谢。

解决方案

scrollToRowAtIndexPath:方法将单元格滚动到UITableViewScrollPositionTop 仅当 tableView的contentSize 大到足以将单元格移动到这些位置时,才会使用 如果您尝试滚动到顶部或中间的单元格是最后一个单元格或最后几个单元格,则不能滚动到中间或顶部。在这些情况下,您需要为这些单元格手动计算 contentOffset



编辑 - 计算contentOffset: / p>

为了计算 contentOffset ,请使用@Schoob指定的方法。在您的 textFieldDidBeginEditing 方法中加入以下代码。

  CGPoint origin = textField.frame.origin; 
CGPoint point = [textField.superview convertPoint:origin toView:self.tableView];
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGPoint offset = tableView.contentOffset;
//根据需要调整以下值
offset.y + =(point.y - navBarHeight);
[tableView setContentOffset:o animated:YES];


I am in a UITableViewController and I have textfields inside cells. When the user clicks on the textfield, I implement the UITextFieldDelegate and in method textFieldDidBeganEditing I determine the index of the cell and scroll to that position. It works perfectly for all cells expect for the last 4-5 cells. What am I doing wrong? Thanks.

解决方案

scrollToRowAtIndexPath: method scrolls the cell to UITableViewScrollPositionTop or UITableViewScrollPositionMiddle only if the tableView's contentSize is large enough to bring the cell to those positions. If the cell you are trying to scroll to top or middle is the last cell or among the last few cells, it can not be scrolled to middle or top. In these situations you need to calculate the contentOffset manually for those cells.

Edit - Calculating the contentOffset:

In order to calculate the contentOffset use the method as specified by @Schoob. Put the following code in your textFieldDidBeginEditing method.

CGPoint origin = textField.frame.origin;
CGPoint point = [textField.superview convertPoint:origin toView:self.tableView];
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGPoint offset = tableView.contentOffset;   
// Adjust the below value as you need
offset.y += (point.y - navBarHeight);
[tableView setContentOffset:o animated:YES];

这篇关于UITableView scrollToRowAtIndexPath不工作的最后4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆