gpt4 book ai didi

ios - 当 ScrollView 滚动时,为什么我的 UIPageControl 没有更新?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:19 25 4
gpt4 key购买 nike

我正在尝试制作一个表格,其单元格中包含 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];


}

最佳答案

那是因为您只引用了为最后一个单元格生成的 ScrollView 和页面控件:

 // 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 委托(delegate)方法中使用 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 对象的引用。为此,您可以使用标签、数组或任何其他内容。

关于ios - 当 ScrollView 滚动时,为什么我的 UIPageControl 没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com