Abbey Jackson
Abbey Jackson

Reputation: 885

Set up page control with uiscrollview that is inside uitableviewcell

I have a UITableViewController. In one of my prototype cells there is a UIScrollView with horizontal paging and a page control. I have set this up in storyboard, laying out all the elements that will display on each page of the UIScrollView, exactly how I have set a scroll view up in the past in a UIViewController. Since this UIScrollView is inside a UITableViewCell, I am confused about how to connect the page control.

1) Should the code to change the page control be in the cell class file? And if so what would that look like?

2) Also do I implement the UIScrollViewDelegate inside the UITableViewController or inside the UITableViewCell class file?

(this is in objective-c)

enter image description here

Upvotes: 1

Views: 2242

Answers (1)

psobko
psobko

Reputation: 1568

Having the UIPageControl code inside of the cell class seems to be the most appropriate choice. The view controller which is displaying the table/cells shouldn't be delegating this type of internal behaviour.

If you're implementing the code in the cell class then the cell class would need to conform to the UIScrollViewDelegate protocol in order to receive the scrollViewDidScroll: message. The table view controller wouldn't be involved.

This is what it the method would look like:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageIndex = self.scrollView.contentOffset.x / CGRectGetWidth(self.scrollView.frame);
    self.pageControl.currentPage = lround(pageIndex);
}

Also make sure to set the pagingEnabled property on the scrollview to YES.

Upvotes: 2

Related Questions