简体   繁体   English

添加单元格后,iOS UITableView不滚动

[英]iOS UITableView is not scrolling after adding cells

I have a UITableView that represents playable rounds in a game. 我有一个UITableView代表游戏中的可玩回合。 On first launch, only two cells are shown in the tableView. 首次启动时,tableView中仅显示两个单元格。 As the user beats levels, more and more rows are added to the tableView. 随着用户超越水平,越来越多的行被添加到tableView中。 I reloadData every time the tableView is displayed (which happens each time the user beats a level). 每次显示tableView时我都会重载数据(每次用户超过一个级别时都会发生一次)。

Once the tableView contains enough cells to require scrolling, the tableView will not scroll and where the user left it. 一旦tableView包含足够的单元格需要滚动,tableView将不会滚动,也不会在用户停留的位置滚动。 It will scroll to reveal that the cells are down below, but when the user releases their finger, the tableView bounces back, not allowing the user to interact with those cells that require scrolling to see. 它将滚动显示下面的单元格,但是当用户松开手指时,tableView会弹回,不允许用户与需要滚动查看的单元格进行交互。 After completing another level and returning back to the tableView, the view will scroll properly. 完成另一个级别并返回tableView后,视图将正确滚动。

When the scrolling issue exists, it is as though the tableView does not realize that it is big enough to require scrolling, so it bounces back to its original position, despite the fact that there are cells that are displayed down below when the user attempts to scroll. 当存在滚动问题时,就好像tableView没有意识到它足够大以至于需要滚动,因此尽管用户在尝试滚动到下面时仍在下方显示该单元格,它还是会弹回其原始位置。滚动。

I do not implement the heightForRowAtIndexPath or heightForHeaderInSection methods in the tableView's delegate, so that should not be interfering. 我没有在tableView的委托中实现heightForRowAtIndexPath或heightForHeaderInSection方法,因此不应有任何干扰。 I have searched this site and Google for similar issues, and have tried a few things including setting the contentSize, but have been unsuccessful. 我已经在该网站和Google上搜索了类似的问题,并尝试了一些操作,包括设置contentSize,但均未成功。

Is there any reason for this lack of scrolling? 缺少滚动是否有任何原因? Or, more importantly, is there a way to fix it? 或者,更重要的是,有没有办法解决它? Thanks! 谢谢!

EDIT: I see some comments asking about the frame. 编辑:我看到一些评论询问有关框架。 I logged the frame of the view the table is in, the frame of the tableView itself, and the tableView's content size in the viewDidAppear method of the view the table i in. 我在表i所在的视图的viewDidAppear方法中记录了表所在的视图的框架,tableView本身的框架以及tableView的内容大小。

Here are the results when the view appears the time before scrolling is necessary. 当视图出现在需要滚动之前的时间时,这是结果。 Scrolling behavior is as expected; 滚动行为符合预期; the user can scroll past the visible area, and it bounces back. 用户可以滚动查看可见区域,然后反弹回来。

view.frame: {{0, 20}, {320, 460}}
tableView.frame: {{0, 145}, {320, 315}}
tableView.contentSize: {320, 308}

Here are the results when the view appears when scrolling should be necessary. 这是在需要滚动时出现视图时的结果。 Scrolling will allow the cells below to appear, but when the user releases their finger, it bounces back when it should not. 滚动将允许显示下面的单元格,但是当用户松开手指时,它会弹回,而当用户不应该弹起时。

view.frame: {{0, 20}, {320, 460}}
tableView.frame: {{0, 145}, {320, 315}}
tableView.contentSize: {320, 352}

Note that the contentSize.height did change by 44, as it should have after a new cell was added. 请注意,contentSize.height确实改变了44,这是在添加新单元格之后应该改变的。 The contentSize.height is now larger than tableView.frame.height, but the scrolling behavior does not reflect this. 现在,contentSize.height大于tableView.frame.height,但是滚动行为并不反映这一点。

The protocol UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to do is to implement the methods -scrollViewWillBeginDragging and -scrollViewDidScroll directly in your UITableViewDelegate implementation and they will be called automatically if the implementation class is set as delegate to your UITableView. 协议UITableViewDelegate符合UIScrollViewDelegate,因此您要做的就是直接在UITableViewDelegate实现中实现-scrollViewWillBeginDragging和-scrollViewDidScroll方法,如果将实现类设置为UITableView的委托,它们将被自动调用。

So in those methods you can implement logic related to showing content of your tableView. 因此,在这些方法中,您可以实现与显示tableView内容相关的逻辑。 Like suppose you said while scrolling it should show new cell ie the top most cell must hide at that time. 就像您假设您在滚动时所说的那样,它应该显示新的单元格,即当时最顶层的单元格必须隐藏。 You could write logic in above method as, 您可以在上述方法中编写逻辑为,

//set your content of tableview to show new cell each time.

OR for scroll direction , you have method - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView, 或者对于滚动方向,您可以使用-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView方法,

// you can set here start and end coordinates and change them accordingly when scroll decelerates.

Writing your code in such way will not make the scroll bounce back. 以这种方式编写代码不会使滚动弹回。 And you will get perfect results. 这样您将获得完美的结果。 Hope this helps. 希望这可以帮助。

 self.tableview.scrollEnable=YES;//Paste this line where initialized UITableView.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM