2012-10-03 2 views
2

셀에 ScrollViews가있는 UIPageControls가있는 테이블을 만들려고합니다. ScrollViews가 작동하고 PageControls가 있지만 아래쪽 셀을 제외하고 스크롤이 발생하면 PageControl은 업데이트되지 않습니다.ScrollView를 스크롤 할 때 내 UIPageControl이 업데이트되지 않는 이유는 무엇입니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.contentView.backgroundColor = [UIColor blueColor]; 
    UIColor *color = [colorsArray objectAtIndex:indexPath.row]; 
    scrollView = [[UIScrollView alloc] init]; 
    [scrollView setDelegate:self]; 
    CGRect screen = [[UIScreen mainScreen] bounds];  
    CGFloat width = CGRectGetWidth(screen); 
    CGFloat height = tableView.rowHeight; 
    scrollView.frame = CGRectMake(0, 0, width, height/1.25); 
    scrollView.contentSize=CGSizeMake(width*3,height/1.25); 
    scrollView.pagingEnabled = YES; 
    [scrollView setBackgroundColor:color]; 
    [scrollView setShowsHorizontalScrollIndicator:NO]; 
    scrollView.clipsToBounds = YES; 
    NSUInteger i; 
    int xCoord=width/25; 
    int yCoord=width/25; 
    int buttonWidth=width/8; 
    int buttonHeight=width/8; 
    int buffer = width; 
    for (i = 1; i <= 4; i++) 
    { 
     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     aButton.backgroundColor = [UIColor blackColor]; 
     aButton.frame  = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight); 
     [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     aButton.tag = i; 
     UIImage *buttonImage = [picsArray objectAtIndex:indexPath.row]; 
     [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
     [scrollView addSubview:aButton]; 

     xCoord += buttonHeight + buffer; 
    } 
    [cell addSubview:scrollView]; 
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height/1.25, width, height/4)]; 
    pageControl.numberOfPages = 3; 
    pageControl.currentPage = 0; 
    pageControl.userInteractionEnabled =YES; 
    [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged]; 
    [cell addSubview:pageControl]; 
    return cell; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 

    // Update the page when more than 50% of the previous/next page is visible 
    CGFloat pageWidth = scrollView.frame.size.width; 
    int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageControl.currentPage = page; 
} 



- (void)pageAction:(UIPageControl *)sender { 
    // update the scroll view to the appropriate page 
    CGRect frame; 
    frame.origin.x = scrollView.frame.size.width * pageControl.currentPage; 
    frame.origin.y = 0; 
    frame.size = scrollView.frame.size; 
[ scrollView scrollRectToVisible:frame animated:YES]; 


} 

답변

1

당신이 참조를 가지고 있기 때문에 단지보기와 마지막 셀에 대해 생성 페이지 제어를 이동하는 것입니다 : 여기 내 코드입니다

// this references are redefined every time new cell is generated 
scrollView = [[UIScrollView alloc] init]; 
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height/1.25, width, height/4)]; 

당신은 당신의 UIScrollView 위임 방법에 sender를 사용을 시도 할 수 있습니다 :

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    // Update the page when more than 50% of the previous/next page is visible 
    CGFloat pageWidth = sender.frame.size.width; 
    int page = floor((sender.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

    // !!! but here is another problem. You should find reference to appropriate pageControl 
    pageControl.currentPage = page; 
} 

그러나 또 하나의 문제가있을 것입니다 : 적절한 UIPageControl 객체에 대한 참조를 얻어야한다. 태그, 배열 또는 그 밖의 용도로 사용할 수 있습니다.

+0

정말 고마워요! 나는 그것을 작동 시켰어. :) –

+0

@ user1714556 당신은 오신 것을 환영합니다. 그때 대답을 수락하는 것을 잊지 마십시오. 고마워요. gl – Nekto

+0

안녕하세요 @nekto. 나는 똑같은 문제를 겪고있다. 어떻게 작동하는지 게시 할 수 있습니까? 감사 – user2985200

관련 문제