2011-05-13 26 views
0

從Apple樣本「PageControl」中,我們可以知道UIPageControl可以用來控制滾動視圖中頁面的移動。可以使用UIPageControl來控制UITableView的移動嗎?

由於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]; 
} 

回答

2

這肯定是可能的,但你爲什麼要這個?表視圖垂直滾動,而頁面控件具有水平佈局,所以它可能看起來/感覺很奇怪。

無論如何,如果你真的想這樣做,請將您的視圖控制器作爲頁面控制的目標:

[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]; 
} 
相關問題