Can I put a pageview controller in a table cell?

I am wondering if I can place the pageview in a table cell. Basically I'm trying to get each table view cell to scroll left / right for more content.

Can I do this by putting the pageview in a table cell? Or is there another way to be able to scroll left / right in a table cell?

+3


source to share


3 answers


You can load another view controller and add its view as a peep of another view controller.

Although not recommended by Apple guidelines:

Each custom view controller object you create is responsible for managing all of the views in one view hierarchy. [...] a one-to-one correspondence between a view controller and the views in its view hierarchy is a key design consideration. You shouldn't use multiple custom view controllers to manage different parts of the same view hierarchy.

For more information on recommendations click here

The best way to do it is to use UIScrollView and UIPageControl like this ...



cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }    

    NSArray *photo = [NSArray arrayWithObjects:[UIImage imageNamed:@"image1.png"], [UIImage imageNamed:@"image2.png"], [UIImage imageNamed:@"image3.png"], nil];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack; //Scroll bar style
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.scrollView setDelegate:self];
    //Show horizontal scroll bar

    self.scrollView.showsVerticalScrollIndicator = YES; //Close vertical scroll bar
    self.scrollView.bounces = YES; //Cancel rebound effect
    self.scrollView.pagingEnabled = YES; //Flat screen
    self.scrollView.contentSize = CGSizeMake(640, 30);

    NSLog(@"LOG");

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 155, 320, 40)];
    self.pageControl.numberOfPages = photo.count;
    self.pageControl.currentPage = 0;
    self.pageControl.backgroundColor = [UIColor redColor];
    [self.pageControl setTintColor:[UIColor whiteColor]];
    [cell.contentView addSubview:self.pageControl];


    for(int i = 0; i < photo.count; i++)
    {
        CGRect frame;
        frame.origin.x = (self.scrollView.frame.size.width *i) + 10;
        frame.origin.y = 0;
        frame.size = CGSizeMake(self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height);

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        [imageView setImage:[photo objectAtIndex:i]];


        [self.scrollView addSubview:imageView];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*photo.count, self.scrollView.frame.size.height);
    }

    [cell.contentView addSubview:self.scrollView];


    return cell;
}

      

renewal PageControl

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;

    //int page = floor((scrollView.contentOffset.x - pageWidth*0.3) / pageWidth) + 1);

    self.pageControl.currentPage = (int)scrollView.contentOffset.x / (int)pageWidth;
    NSLog(@"CURRENT PAGE %d", self.pageControl.currentPage);
}

      

+3


source


Yes, you can.

Remember, however, that you cannot get rid of the black background to the page control if you choose to go that route. I did this a couple of days ago but ended up ditching the pageview controller in favor of using it UIScrollView

with paging and enabled UIPageControl

. I am using the -scrollViewWillEndDragging:withVelocity:targetContentOffset:

scroll view delegate method to determine when to define currentPage

in a page control.



If you have quite a few views in the scroll view, it would be more memory efficient to view the collection, rather than the normal scroll view.

+2


source


Instead of using a UIPageview, try placing another UITableview (rotated 90 degrees counterclockwise) inside the cell. This will give you more control over touch events and additional customization.

1) Have a class for UITableViewCell and xib file like PageViewTableCell

2) Xib file must have UITableView inside cell

3) Rotate the table when awakeFromNib is called

- (void)awakeFromNib
  {
      horizontalTable.transform = CGAffineTransformMakeRotation(-M_PI_2);
      horizontalTable.frame = CGRectMake(0,0,horizontalTable.frame.size.width,horizontalTable.frame.size.height);
  }

      

4) Have a delegate and data source for the horizontalTable as per your design.

5) Load the main table which has the above cell configured

- (void)viewDidLoad
{
    [self.mainTable registerNib:[UINib nibWithNibName:@"PageViewTableCell"
                                          bundle:[NSBundle mainBundle]]
    forCellReuseIdentifier:@"samplecell"];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PageViewTableCell* cell = [tableView dequeueReusableCellWithIdentifier:@"samplecell"];
    return cell;
}

      

0


source







All Articles