UIScrollView and UIPageControl within UITableView

前端 未结 1 647
广开言路
广开言路 2021-01-07 11:09

I\'ve seen lots of sources saying it is possible to include a UIScrollView with UIPageControl inside a UITableViewCell to be able to scroll horizont

相关标签:
1条回答
  • 2021-01-07 11:41

    I've solved my own problem; maybe the reason no once answered me is because its a minor implementation once you understand each view's purpose.

    From within cellForRowAtIndexPath: I created a standard UITableViewCell, however I altered the frame of the cell to my own custom frame of 1000 width by 50 height (catered to my needs for project).

    I then created a UIScrollView, set it to the following (keep in mind I have my tableView defined in IB, so I'm mapping some of my height/widths to those values):

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
    

    I then create the desired image view (I realize I will next create a loop that does many images and lays them out across the scroll view):

    UIImage *image = [UIImage imageNamed:@"dummy.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, 80, 78);
    [scrollView addSubview: imageView];
    

    Here's the part I was missing. After adding the scrollView to the cell contents, you need to use the UIPageControl (which didn't seem obvious to me for this implementation at first) to setup the actual "visual horizonal scrolling" affect:

       [[cell contentView] addSubview:scrollView];
    
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
    [pageControl setNumberOfPages:4];
    [[cell contentView] addSubview:pageControl];
    

    Hope that helps someone's search - I spent quite some time on Google looking for the example I just explained and didn't have much luck other than the general overview of how it would work.

    0 讨论(0)
提交回复
热议问题