是否可以使用 UIPageControl 来控制 UITableView 的移动

Is that possible to use UIPageControl to control the move of UITableView?(是否可以使用 UIPageControl 来控制 UITableView 的移动?)
本文介绍了是否可以使用 UIPageControl 来控制 UITableView 的移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Apple 示例PageControl"我们可以知道 UIPageControl 可用于控制页面在滚动视图中的移动.

From Apple sample "PageControl" we can know that UIPageControl can be used to control movement of pages in scrollview.

由于 UITableView 是 UIScrollView 的子类,我想使用 UIPageControl 来控制表格视图单元格的移动,就像在PageControl"中一样.

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.

问题是:

可以这样做吗?为什么?

谢谢

让我回答我的问题:

以编程方式选择和滚动

清单 6-5 以编程方式选择行

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

推荐答案

确实有可能,但你为什么想要这个?表格视图垂直滚动,而页面控件具有水平布局,因此它可能看起来/感觉很奇怪.

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:

[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];

然后在action中实现滚动:

Then implement the scrolling in the action:

- (void)pageChanged:(UIPageControl *)sender {
    float scrollPosition = ((float)sender.currentPage / (float)sender.numberOfPages) * tableView.contentSize.height;
    [tableView scrollRectToVisible:CGRectMake(0, scrollPosition, 1, 1) animated:YES];
}

这篇关于是否可以使用 UIPageControl 来控制 UITableView 的移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)