有可能使用UIPageControl来控制UITableView的移动吗?

从苹果示例“PageControl”中,我们可以知道UIPageControl可以用来控制滚动视图中页面的移动。

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

但是找不到代理的任何接口。

问题是:

这是可能的吗? 为什么?

谢谢

让我回答我的问题:

以编程方式select和滚动

清单6-5以编程方式select一行

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

这当然是可能的,但你为什么要这样? 一个表视图垂直滚动,而一个页面控件有一个水平的布局,所以它可能会看起来/感觉怪异。

无论如何,如果你真的想这样做,添加您的视图控制器作为页面控制的目标:

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