UITageControl和UIScrollView在UITableViewCell中

时间:2015-06-17 02:19:23

标签: ios uitableview uiscrollview uipagecontrol

我创建了一个自定义UITableViewCell,它有一个UIScrollView和一个UIPageControl。我已将下面的图片附在如何设置上。一旦我在表格中使用它,我会看到2个问题。它加载了10个图像,UIPageControl有10个点。

  1. 当我点击其中一个pageControl点时,它会进入UITableView的didSelectCell方法

  2. 当我尝试滚动UIScrollView时,它会滚动,但随后它会崩溃并返回以下跟踪:

    • 线程#1:tid = 0x134dc1,0x000000010a10b00b libobjc.A.dylib objc_msgSend + 11, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) frame #0: 0x000000010a10b00b libobjc.A.dylib objc_msgSend + 11 帧#1:0x00000001077b6a9a UIKit -[UIScrollView(UIScrollViewInternal) _scrollViewWillBeginDragging] + 117 frame #2: 0x00000001077a686f UIKit - [UIScrollView _updatePanGesture] + 238 帧#3:0x0000000107ac77b6 UIKit _UIGestureRecognizerSendActions + 262 frame #4: 0x0000000107ac6459 UIKit - [UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532 帧#5:0x0000000107acb076 UIKit ___UIGestureRecognizerUpdate_block_invoke662 + 51 frame #6: 0x0000000107acaf72 UIKit _ UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254 第7帧:0x0000000107ac0fed UIKit`_UIGestureRecognizerUpdate + 2796
  3. 关于我做错的任何指示非常感谢!

    enter image description here

1 个答案:

答案 0 :(得分:1)

它不应该转到didSelectCell方法我认为您需要为page control设置操作,例如

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