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

Ios 是否可以使用UIPageControl来控制UITableView的移动?,ios,uitableview,ios4,uiscrollview,uipagecontrol,Ios,Uitableview,Ios4,Uiscrollview,Uipagecontrol,从Apple示例“PageControl”中,我们可以知道UIPageControl可用于控制scrollview中页面的移动 由于UITableView是UIScrollView的子类,我想使用UIPageControl来控制表视图单元格的移动,就像“PageControl”中一样 但找不到任何委托接口供我这样做 问题是: 这样做可能吗?为什么呢? 谢谢 让我回答我的问题: 以编程方式选择和滚动 清单6-5以编程方式选择一行 - (void)tableView:(UITableView *)t

从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];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)newindepath{
nsindepath*scrollIndexPath;
如果(newindexath.row+20<[timeZoneNames计数]){
scrollIndexPath=[NSIndexPath indexPathForRow:newIndexPath.row+20片段:newIndexPath.section];
}否则{
scrollIndexPath=[NSIndexPath indexPathForRow:newIndexPath.row-20片段:newIndexPath.section];
}
[表格视图选择行索引路径:滚动索引:是]
scrollPosition:UITableViewScrollPositionMiddle];
}

这当然是可能的,但您为什么要这样做?表视图垂直滚动,而页面控件具有水平布局,因此可能看起来/感觉很奇怪

无论如何,如果确实要执行此操作,请将视图控制器添加为页面控件的目标:

[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
然后在操作中执行滚动:

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

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

无论如何,如果确实要执行此操作,请将视图控制器添加为页面控件的目标:

[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
然后在操作中执行滚动:

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