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

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

问题描述

在我正在开发的应用程序中,我有一个普通样式的 UITableView,它可以包含一个包含零行的部分.我希望能够使用 scrollToRowAtIndexPath:atScrollPosition:animated: 滚动到此部分,但由于缺少子行,当我尝试滚动到此部分时出现错误.

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 的日历应用程序能够执行此操作,如果您在列表视图中查看日历,并且您的日历中没有今天的事件,则会为今天插入一个空白部分,您可以使用今天"按钮滚动到该部分在屏幕底部的工具栏中.据我所知,Apple 可能正在使用自定义的 UITableView,或者他们正在使用私有 API...

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...

我能想到的唯一解决方法是在 0 像素高的地方插入一个空的 UITableCell 并滚动到那个位置.但我的理解是,具有不同高度的单元格对滚动性能非常不利.无论如何我还是会尝试一下,也许性能上的打击不会太糟糕.

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.

更新

由于似乎没有解决方案,我已经向苹果提交了错误报告.如果这也影响到您,请提交 rdar://problem/6263339 的副本(打开雷达链接) 如果你想让它更快地解决这个问题.

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.

更新 #2

我有一个不错的解决方法来解决这个问题,请看下面我的回答.

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

推荐答案

UPDATE:看起来这个 bug 在 iOS 3.0 中已修复.您可以使用以下 NSIndexPath 滚动到包含 0 行的部分:

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]

我将把我原来的解决方法留在这里,供仍然使用 2.x SDK 维护项目的任何人使用.

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];

上面的代码将滚动 tableview,因此所需的部分是可见的,但不一定在可见区域的顶部或底部.如果您想滚动以使该部分位于顶部,请执行以下操作:

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];

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

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

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

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