如何将UITableView滚动到不包含行的部分? [英] How do I scroll a UITableView to a section that contains no rows?

查看:110
本文介绍了如何将UITableView滚动到不包含行的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的应用程序中,我有一个简单的样式UITableView,可以包含一个包含零行的部分。我想使用scrollToRowAtIndexPath:atScrollPosition:animated:但我得到一个错误,当我试图滚动到这个部分由于缺少子行。



Apple的日历应用程序可以执行此操作,如果您在列表视图中查看日历,并且今天的日历中没有任何事件,则今天会插入一个空白部分,您可以使用今日按钮滚动到该部分在屏幕底部的工具栏中。就我可以告诉苹果可能正在使用一个自定义的UITableView,或者他们正在使用私人API ...



我可以想到的唯一解决方法是插入一个空的UITableCell在0像素高,滚动到那。但是我的理解是,有不同高度的单元格真的不好滚动性能。



更新



由于似乎没有解决方案,我已经提交了一个bug报告与苹果。如果这也影响到您,请提交一份rdar:// problem / 6263339( Open Radar链接)的副本,



$ b

解决方案

更新:看起来像这样:这个bug在iOS 3.0中是固定的。您可以使用以下 NSIndexPath 滚动到包含0行的部分:

  [NSIndexPath indexPathForRow:NSNotFound inSection:section] 

仍然使用2.x SDK维护项目。






找到了一个正确的解决方法:

  CGRect sectionRect = [tableView rectForSection:indexOfSectionToScrollTo]; 
[tableView scrollRectToVisible:sectionRect animated:YES];

上面的代码将滚动tableview,所以所需的部分是可见的,但不一定在顶部或底部的可见区域。如果你想滚动所以部分在顶部,这样做:

  CGRect sectionRect = [tableView rectForSection:indexOfSectionToScrollTo]; 
sectionRect.size.height = tableView.frame.size.height;
[tableView scrollRectToVisible:sectionRect animated:YES];

根据需要修改sectionRect,将所需的部分滚动到可见区域的底部或中间。 p>

In an app I'm working on, I have a plain style UITableView that can contain a section containing zero rows. I want to be able to scroll to this section using scrollToRowAtIndexPath:atScrollPosition:animated: but I get an error when I try to scroll to this section due to the lack of child rows.

Apple's calendar application is able to do this, if you look at your calendar in list view, and there are no events in your calendar for today, an empty section is inserted for today and you can scroll to it using the Today button in the toolbar at the bottom of the screen. As far as I can tell Apple may be using a customized UITableView, or they're using a private API...

The only workaround I can think of is to insert an empty UITableCell in that's 0 pixels high and scroll to that. But it's my understanding that having cells of varying heights is really bad for scrolling performance. Still I'll try it anyway, maybe the performance hit won't be too bad.

Update

Since there seems to be no solution to this, I've filed a bug report with apple. If this affects you too, file a duplicate of rdar://problem/6263339 (Open Radar link) if you want this to get this fixed faster.

Update #2

I have a decent workaround to this issue, take a look at my answer below.

解决方案

UPDATE: Looks like this bug is fixed in iOS 3.0. You can use the following NSIndexPath to scroll to a section containing 0 rows:

[NSIndexPath indexPathForRow:NSNotFound inSection:section]

I'll leave my original workaround here for anyone still maintaining a project using the 2.x SDK.


Found a decent workaround:

CGRect sectionRect = [tableView rectForSection:indexOfSectionToScrollTo];
[tableView scrollRectToVisible:sectionRect animated:YES];

The code above will scroll the tableview so the desired section is visible but not necessarily at the top or bottom of the visible area. If you want to scroll so the section is at the top do this:

CGRect sectionRect = [tableView rectForSection:indexOfSectionToScrollTo];
sectionRect.size.height = tableView.frame.size.height;
[tableView scrollRectToVisible:sectionRect animated:YES];

Modify sectionRect as desired to scroll the desired section to the bottom or middle of the visible area.

这篇关于如何将UITableView滚动到不包含行的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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