2

我正在尝试制作一个表格,其单元格中包含 UIPageControls 和 ScrollViews。ScrollViews 工作并且 PageControls 在那里,但是当滚动发生时,除了底部单元格之外,PageControl 不会更新。这是我的代码:

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


}
4

1 回答 1

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

您可以尝试sender在您的UIScrollView委托方法中使用:

- (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对象的引用。您可以为此目的使用标签、数组或其他任何东西。

于 2012-10-04T02:19:13.650 回答