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

3
我想知道是否可以在表格视图单元格中放置一个页面视图。基本上,我正在尝试使每个表格视图单元格能够左右滚动以获取更多内容。
我能否通过在表格视图单元格中放置一个页面视图来实现它?还是有另一种方法可以在表格视图单元格中左右滑动?

@rmaddy,是的,我知道我可以使用页面控制器(实际上不需要将整个视图控制器放入其中),但我想知道这是否实用。 - Joseph
3个回答

3

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

尽管这不是苹果指南推荐的做法:

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

有关指南的更多信息,请单击此处

更好的方法是使用UIScrollView和UIPageControl,像这样...

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

更新 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

可以做到。但是请注意,如果您选择这样做,您无法摆脱页面控件中的黑色背景。几天前我尝试过这种方法,但最终放弃了页面视图控制器,改用启用分页和UIPageControlUIScrollView。我使用-scrollViewWillEndDragging:withVelocity:targetContentOffset:滚动视图委托方法来确定何时在页面控件中确定currentPage

如果您将在滚动视图中拥有相当多的视图,则使用集合视图而不是普通滚动视图会更节省内存。


0

不要使用UIPageview,尝试将另一个UITableview(逆时针旋转90度)放在单元格内。这将使您对触摸事件有更多控制,并且还可以进行更多自定义。

1)为UITableViewCell和xib文件创建一个类,例如PageViewTableCell

2)xib文件应该在单元格内有一个UITableView

3)当awakeFromNib被调用时旋转表格

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接