Is that possible to use UIPageControl to control t

2020-05-08 19:06发布

问题:

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