問題描述
從 Apple 示例PageControl"我們可以知道 UIPageControl 可用于控制頁面在滾動視圖中的移動.
From Apple sample "PageControl" we can know that UIPageControl can be used to control movement of pages in scrollview.
由于 UITableView 是 UIScrollView 的子類,我想使用 UIPageControl 來控制表格視圖單元格的移動,就像在PageControl"中一樣.
As UITableView is subclass of UIScrollView,I want to use UIPageControl to control the movement of table view cell just like in "PageControl".
但是找不到任何委托接口讓我這樣做.
But can not find any interface of delegate for me to do that.
問題是:
可以這樣做嗎?為什么?
謝謝
讓我回答我的問題:
以編程方式選擇和滾動
清單 6-5 以編程方式選擇行
Listing 6-5 Programmatically selecting a row
- (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];
}
推薦答案
確實有可能,但你為什么想要這個?表格視圖垂直滾動,而頁面控件具有水平布局,因此它可能看起來/感覺很奇怪.
It sure is possible, but why would you want this? A table view scrolls vertically, while a page control has a horizontal layout, so it would probably look/feel weird.
無論如何,如果您真的想這樣做,請將您的視圖控制器添加為頁面控件的目標(biāo):
Anyway, if you really want to do this, add your view controller as a target of the page control:
[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
然后在action中實現(xiàn)滾動:
Then implement the scrolling in the 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];
}
這篇關(guān)于是否可以使用 UIPageControl 來控制 UITableView 的移動?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!