Is that possible to use UIPageControl to control the move of UITableView?

不问归期 提交于 2019-12-04 07:13:52

问题


From Apple sample "PageControl" we can know that UIPageControl can be used to control movement of pages in scrollview.

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.

Question is :

Is that possible to do this ? And Why ?

Thanks

Let me answer my question with :

Programmatically Selecting and Scrolling

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

回答1:


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.

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

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


来源:https://stackoverflow.com/questions/5988664/is-that-possible-to-use-uipagecontrol-to-control-the-move-of-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!