我可以在表视图单元格中放置一个页面视图控制器吗?

时间:2022-03-30 03:47:02

I'm wondering if I could put a page view within a table view cell. Basically I'm trying to get each table view cell to be able to scroll left/right for more content.

我想知道我是否可以在表视图单元格中放置一个页面视图。基本上,我试着让每个表视图单元格能够滚动左右以获得更多的内容。

Can I do it by putting a page view within a table view cell? Or is there another way to be able to swipe left/right in a table view cell?

我可以在表视图单元格中放置一个页面视图吗?或者还有其他方法可以在表视图单元格中向左/向右滑动?

3 个解决方案

#1


3  

It's possible to load another view controller and add its view as a subview of another controller's view.

可以加载另一个视图控制器,并将其视图添加为另一个控制器视图的子视图。

Although it's not recommended by Apple's guidelines:

尽管苹果的指导方针并不推荐它:

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

您创建的每个自定义视图控制器对象负责管理单个视图层次结构中的所有视图。[…视图控制器与其视图层次结构中的视图之间的一对一通信是设计考虑的关键。不应该使用多个自定义视图控制器来管理相同视图层次结构的不同部分。

More info on the guidelines click here

更多有关指南的信息请点击这里

A better way to do it is using a UIScrollView and a UIPageControl like this…

更好的方法是使用UIScrollView和UIPageControl…

cellForRowAtIndexPath

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;
}

update PageControl

更新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);
}

#2


2  

Yes, you can.

是的,你可以。

However, be aware that you can't get rid of the black background to the page control in it if you choose to go that route. I did this a couple days ago but ended up getting rid of the page view controller in favor of using a UIScrollView that has paging enabled and a UIPageControl. I use the -scrollViewWillEndDragging:withVelocity:targetContentOffset: scroll view delegate method to determine when to determine the currentPage in the page control.

但是,请注意,如果您选择使用该路径,则无法摆脱页面控件的黑色背景。我在几天前做了这个,但最终摆脱了页面视图控制器,转而使用具有分页功能和UIPageControl的UIScrollView。我使用- scrollviewwillenddrag:withVelocity:targetContentOffset:滚动视图委托方法来确定何时确定页面控件中的currentPage。

If you're going to have quite a few views in the scroll view, it would be more memory efficient to use a collection view instead of a normal scroll view.

如果在滚动视图中有很多视图,那么使用集合视图而不是普通的滚动视图会更节省内存。

#3


0  

Instead of using a UIPageview, try to put another UITableview (rotated by 90 degrees in anticlockwise direction)inside a cell. This will give you more control over touch events and more customization too.

不要使用UIPageview,尝试将另一个UITableview(逆时针旋转90度)放在单元格中。这将给您更多的控制触摸事件和更多的定制。

1) Have a class for UITableViewCell and an xib file too, say PageViewTableCell

1)有一个UITableViewCell类和一个xib文件,比如PageViewTableCell

2) Xib file should have a UITableView inside the cell

2) Xib文件在单元格中应该有一个UITableView

3) Rotate the table when awakeFromNib is called

3)调用awakeFromNib时旋转表

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

4) Have the delegate and datasource for horizontalTable according to your design.

4)根据您的设计为horizontalTable设置委托和数据源。

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

5)加载具有上述自定义单元格的主表

- (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;
}

#1


3  

It's possible to load another view controller and add its view as a subview of another controller's view.

可以加载另一个视图控制器,并将其视图添加为另一个控制器视图的子视图。

Although it's not recommended by Apple's guidelines:

尽管苹果的指导方针并不推荐它:

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

您创建的每个自定义视图控制器对象负责管理单个视图层次结构中的所有视图。[…视图控制器与其视图层次结构中的视图之间的一对一通信是设计考虑的关键。不应该使用多个自定义视图控制器来管理相同视图层次结构的不同部分。

More info on the guidelines click here

更多有关指南的信息请点击这里

A better way to do it is using a UIScrollView and a UIPageControl like this…

更好的方法是使用UIScrollView和UIPageControl…

cellForRowAtIndexPath

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;
}

update PageControl

更新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);
}

#2


2  

Yes, you can.

是的,你可以。

However, be aware that you can't get rid of the black background to the page control in it if you choose to go that route. I did this a couple days ago but ended up getting rid of the page view controller in favor of using a UIScrollView that has paging enabled and a UIPageControl. I use the -scrollViewWillEndDragging:withVelocity:targetContentOffset: scroll view delegate method to determine when to determine the currentPage in the page control.

但是,请注意,如果您选择使用该路径,则无法摆脱页面控件的黑色背景。我在几天前做了这个,但最终摆脱了页面视图控制器,转而使用具有分页功能和UIPageControl的UIScrollView。我使用- scrollviewwillenddrag:withVelocity:targetContentOffset:滚动视图委托方法来确定何时确定页面控件中的currentPage。

If you're going to have quite a few views in the scroll view, it would be more memory efficient to use a collection view instead of a normal scroll view.

如果在滚动视图中有很多视图,那么使用集合视图而不是普通的滚动视图会更节省内存。

#3


0  

Instead of using a UIPageview, try to put another UITableview (rotated by 90 degrees in anticlockwise direction)inside a cell. This will give you more control over touch events and more customization too.

不要使用UIPageview,尝试将另一个UITableview(逆时针旋转90度)放在单元格中。这将给您更多的控制触摸事件和更多的定制。

1) Have a class for UITableViewCell and an xib file too, say PageViewTableCell

1)有一个UITableViewCell类和一个xib文件,比如PageViewTableCell

2) Xib file should have a UITableView inside the cell

2) Xib文件在单元格中应该有一个UITableView

3) Rotate the table when awakeFromNib is called

3)调用awakeFromNib时旋转表

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

4) Have the delegate and datasource for horizontalTable according to your design.

4)根据您的设计为horizontalTable设置委托和数据源。

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

5)加载具有上述自定义单元格的主表

- (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;
}