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

标签 ios uitableview ios4 uiscrollview uipagecontrol

从Apple示例“PageControl”我们可以知道,UIPageControl可以用来控制页面在scrollview中的移动。

由于 UITableView 是 UIScrollView 的子类,我想使用 UIPageControl 来控制表格 View 单元格的移动,就像在“PageControl”中一样。

但是找不到任何委托(delegate)接口(interface)供我执行此操作。

问题是:

这有可能吗?为什么?

谢谢

让我回答我的问题:

以编程方式选择和滚动

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

最佳答案

这当然是可能的,但你为什么想要这个? TableView 垂直滚动,而页面控件具有水平布局,因此看起来/感觉起来可能很奇怪。

无论如何,如果您真的想这样做,请将您的 View Controller 添加为页面控件的目标:

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

然后在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];
}

关于ios - 是否可以使用 UIPageControl 来控制 UITableView 的移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5988664/

相关文章:

ios - 域异常错误Domain = io.realm代码= 2“不允许操作”

ios - 在 ios 中使用 sdk 时自定义谷歌登录按钮

ios - 奇怪的 UITable Separator 问题 - 右移

ios:如何以编程方式从细节到 TableView

iphone - performSelector :withObject:afterDelay: and dispatch_after 之间的权衡是什么

iphone - 如何以编程方式完全像这个 UIButton 布局?

ios - MagicalRecord saveInBackgroundWithBlock 不保存对象

ios - 我应该如何构建这个 View ?

iphone - 编辑 TableView 时自定义 TableCell 中没有缩进

ios - 如何从 NSNotification 对象获取对象(核心数据)