UIPageControl and UIScrollView in a UITableViewCell

1k views Asked by At

I have created a custom UITableViewCell which has a UIScrollView and a UIPageControl. I have attached the image below on how it is setup. Once I use this inside a table I see 2 issues. It loads up 10 images, the UIPageControl has 10 dots.

  1. When I tap on one of the pageControl dots, it goes into the UITableView's didSelectCell method

  2. When I try to scroll the UIScrollView sometime it scrolls, but then it crashes with the following back trace :

    • thread #1: tid = 0x134dc1, 0x000000010a10b00b libobjc.A.dylibobjc_msgSend + 11, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) frame #0: 0x000000010a10b00b libobjc.A.dylibobjc_msgSend + 11 frame #1: 0x00000001077b6a9a UIKit-[UIScrollView(UIScrollViewInternal) _scrollViewWillBeginDragging] + 117 frame #2: 0x00000001077a686f UIKit-[UIScrollView _updatePanGesture] + 238 frame #3: 0x0000000107ac77b6 UIKit_UIGestureRecognizerSendActions + 262 frame #4: 0x0000000107ac6459 UIKit-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532 frame #5: 0x0000000107acb076 UIKit___UIGestureRecognizerUpdate_block_invoke662 + 51 frame #6: 0x0000000107acaf72 UIKit_UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254 frame #7: 0x0000000107ac0fed UIKit`_UIGestureRecognizerUpdate + 2796

Any pointers on what I am doing wrong very much appreciated!

enter image description here

1

There are 1 answers

0
Shankar BS On

it should not go to didSelectCell method i think u need to set action for page control for example

in customCell

- (void)awakeFromNib {
   // Initialization code
   [self setUpScrollView];
   self.aScrollView.pagingEnabled = YES;
   self.aScrollView.delegate = self;
   [self.pageControl addTarget:self action:@selector(turnPage:) forControlEvents:UIControlEventValueChanged];
   self.pageControl.currentPage = 0;
   self.pageControl.numberOfPages = 5;
 }

 - (void)setUpScrollView
 {
   self.aScrollView.contentSize = CGSizeMake(self.aScrollView.bounds.size.width * 5,self.aScrollView.bounds.size.height);
   for(int i = 0 ; i < 5 ; i++)
   {
      UIImageView *aImageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.aScrollView.bounds.size.width, 0, self.aScrollView.bounds.size.width, self.aScrollView.bounds.size.height)];
      aImageView.contentMode = UIViewContentModeScaleAspectFit; //my images are differnt aspect ratio
      aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
      aImageView.backgroundColor = [UIColor greenColor];
      [self.aScrollView addSubview:aImageView];
  }
}

//hear u set the correct page for scrollview and set the current page
- (void)turnPage:(UIPageControl *)aPageControl
{
   NSInteger selectedPage  = self.pageControl.currentPage;
   CGRect frame = self.bounds;
   frame.origin.x = frame.size.width * selectedPage;
   frame.origin.y = 0;
   [self.aScrollView scrollRectToVisible:frame animated:YES];
   _pageControl.currentPage = selectedPage;
}

 //after at the end of scroll u need update the current page in the page control 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  CGFloat pageWidth = self.aScrollView.frame.size.width;
  int page = floor((self.aScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  NSLog(@"Current page -> %d",page);
  CGFloat xoffset = page * self.aScrollView.bounds.size.width;
  self.aScrollView.contentOffset = CGPointMake(xoffset, 0);
  self.pageControl.currentPage = page;
}