从Apple示例“PageControl”中我们可以知道UIPageControl可以用来控制scrollview中页面的移动。
由于 UITableView 是 UIScrollView 的子类,我想使用 UIPageControl 来控制表视图单元格的移动,就像在“PageControl”中一样。
但找不到任何委托接口供我执行此操作。
问题是:
有可能做到这一点吗?为什么?
Thanks
让我回答我的问题:
以编程方式选择和滚动
清单 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];
}
这当然是可能的,但你为什么想要这个呢?表视图垂直滚动,而页面控件具有水平布局,因此它可能看起来/感觉很奇怪。
无论如何,如果您确实想这样做,请将视图控制器添加为页面控件的目标:
[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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)