是否可以使用UIPageControl来控制UITableView的移动?
从Apple示例“PageControl”中我们可以知道UIPageControl可以用来控制scrollview中页面的移动。
由于 UITableView 是 UIScrollView 的子类,我想使用 UIPageControl 来控制表视图单元格的移动,就像在“PageControl”中一样。
但找不到任何委托接口供我执行此操作。
问题是:
可以这样做吗?为什么?
谢谢
让我回答我的问题:
以编程方式选择和滚动
清单 6-5 以编程方式选择一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
NSIndexPath *scrollIndexPath;
if (newIndexPath.row + 20 < [timeZoneNames count]) {
scrollIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row+20 inSection:newIndexPath.section];
} else {
scrollIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row-20 inSection:newIndexPath.section];
}
[theTableView selectRowAtIndexPath:scrollIndexPath animated:YES
scrollPosition:UITableViewScrollPositionMiddle];
}
From Apple sample "PageControl" we can know that UIPageControl can be used to control movement of pages in scrollview.
As UITableView is subclass of UIScrollView,I want to use UIPageControl to control the movement of table view cell just like in "PageControl".
But can not find any interface of delegate for me to do that.
Question is :
Is that possible to do this ? And Why ?
Thanks
Let me answer my question with :
Programmatically Selecting and Scrolling
Listing 6-5 Programmatically selecting a row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
NSIndexPath *scrollIndexPath;
if (newIndexPath.row + 20 < [timeZoneNames count]) {
scrollIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row+20 inSection:newIndexPath.section];
} else {
scrollIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row-20 inSection:newIndexPath.section];
}
[theTableView selectRowAtIndexPath:scrollIndexPath animated:YES
scrollPosition:UITableViewScrollPositionMiddle];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这当然是可能的,但你为什么想要这个呢?表视图垂直滚动,而页面控件具有水平布局,因此它可能看起来/感觉很奇怪。
无论如何,如果您确实想这样做,请将视图控制器添加为页面控件的目标:
然后在操作中实现滚动:
It sure is possible, but why would you want this? A table view scrolls vertically, while a page control has a horizontal layout, so it would probably look/feel weird.
Anyway, if you really want to do this, add your view controller as a target of the page control:
Then implement the scrolling in the action: