我正在尝试制作一个表,其单元格具有带有ScrollViews的UIPageControls。 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];


}

最佳答案

那是因为您仅参考为最后一个单元格生成的滚动视图和页面控件:

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

10-08 06:09