从苹果的示例"PageControl“中我们可以知道,UIPageControl可以用来控制滚动视图中页面的移动。
由于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];
}发布于 2011-05-13 07:48:44
这当然是可能的,但你为什么要这样?表视图垂直滚动,而页控件具有水平布局,因此它可能看起来/感觉很奇怪。
无论如何,如果您真的想这样做,请将视图控制器添加为页面控件的目标:
[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];
}https://stackoverflow.com/questions/5988664
复制相似问题