UIPageControl Dots设计凌乱

UIPageControl Dots design messy

click here to watch video.

我正在使用 ALScrollViewPaging 库进行图像分页,我将这段代码集成到 UITableviewCell 中。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

PoastCell *cell;
 cell=[tblBoardDetail dequeueReusableCellWithIdentifier:@"PoastCell" forIndexPath:indexPath];
NSDictionary *dict=[arrPosts objectAtIndex:indexPath.row];

ALScrollViewPaging *scrollView = [[ALScrollViewPaging alloc] initWithFrame: CGRectMake (0, 0, self.view.frame.size.width, 200)];
    scrollView.showsHorizontalScrollIndicator=NO;
    NSMutableArray *views = [[NSMutableArray alloc] init];        
    for (NSString *strImages in [dict valueForKey:@"post_image"])
    {
        UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,200)];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode=UIViewContentModeScaleAspectFill;
        [Common loadImgFromUrl:strImages :imageView];
        imageView.frame = catView.bounds;
        [catView addSubview:imageView];
        [views addObject:catView];
    }
    [scrollView addPages:views];
    [cell.imagePagingView addSubview:scrollView];
    [scrollView setHasPageControl:YES];

    }

我建议您以编程方式制作带有页面控件的 Scrollview,这比使用库更容易。

制作一个页面控件属性:

@property (nonatomic, weak)UIPageControl *pageControl;

不要忘记将 class 设置为 UIScrollViewDelegate

@interface ViewController() <UIScrollViewDelegate>

然后在您的单元格中创建一个滚动视图:

//scrollview
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 200);
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:scrollViewFrame];
[scrollView setPagingEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:NO];

// i is the number of images you want to display
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * i, scrollView.bounds.size.height);
scrollView.delegate = self;

//page control. i is equals to number of dots you want
self.pageControl = [[UIPageControl alloc] init];
CGRect pageControlFrame = CGRectMake(0.0, 0.0, 0.0, 0.0);
self.pageControl.frame = pageControlFrame;
//i is equals to number of dots you want
self.pageControl.numberOfPages = i;

NSDictionary *dict=[arrPosts objectAtIndex:indexPath.row];
for (NSString *strImages in [dict valueForKey:@"post_image"]) {
    //imageview
    CGRect imageViewFrame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 200);
    UIImageView *imageContainer = [[UIImageView alloc] initWithFrame:imageViewFrame];
    imageContainer.image = SET_YOUR_IMAGE_HERE;
    imageContainer.contentMode = UIViewContentModeScaleAspectFill;
    CGRect frame = scrollView.frame;
    frame.origin.x = scrollViewFrame.size.width * i;
    imageContainer.frame = frame;

    [scrollView addSubview:imageContainer];
}

[cell addSubview:self.pageControl];
[cell addSubview:scrollView];
}

//this highlight the dot of the page you scroll to
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageControl.currentPage = page;
}