【问题标题】:UIScrollView and UIPageControl within UITableViewUITableView 中的 UIScrollView 和 UIPageControl
【发布时间】:2011-07-02 19:24:49
【问题描述】:

我看到很多消息来源说可以在 UITableViewCell 中包含带有 UIPageControl 的 UIScrollView 以便能够水平滚动(通过可选图像列表),但找不到任何我想要的原始示例。我的滚动视图“工作”了,但我不确定如何继续 - 希望有人可以给我一些指导。

在我的 cellForRowAtIndexPath 中,我创建了一个单元格,并将 scrollView 和 pageControl 添加为子视图。

UITableViewCell *cell = [self.challengeListView dequeueReusableCellWithIdentifier:SubmitChallengeCellIdentifier];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SubmitChallengeCellIdentifier] autorelease];
        cell.frame = CGRectMake(0, 0, 1000, 50);
    }   

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 50)];
    [scrollView setContentSize:CGSizeMake(1000, 50)];
    [[cell contentView] addSubview:scrollView];

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

我附上了正在显示的屏幕截图

主视图的底部是我的 UITableView,其中包含 scrollView/pageControl(它会水平滚动,因为我可以看到 scrollerIndicator 显示了这一点),我将它的方法设置为以下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   
    return 1;
}   

我的滚动视图将显示一个“horizo​​ntalScroller”,我可以来回滚动,但显然那里没有内容如何使用“可点击”图像列表填充 tableView/scrollView?任何方向都将不胜感激 - 最好不是“嘿,这家伙做的有点相似,看看这个链接”,但也许更多的是解释如何正确实现这个功能(E特别是关于iOS 3.0+ - 据我了解,Apple 在实现这一点时让我们的生活变得更轻松)

【问题讨论】:

    标签: iphone xcode uitableview uiscrollview uipagecontrol


    【解决方案1】:

    我已经解决了我自己的问题;也许 no once 回答我的原因是因为一旦你理解了每个视图的目的,它就是一个次要的实现。

    来自cellForRowAtIndexPath: 我创建了一个标准的UITableViewCell,但是我将单元格的框架更改为我自己的 1000 宽 x 50 高的自定义框架(满足我对项目的需要)。

    然后我创建了一个UIScrollView,将其设置为以下内容(请记住,我在 IB 中定义了我的 tableView,因此我将我的一些高度/宽度映射到这些值):

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

    然后我创建所需的图像视图(我意识到接下来我将创建一个循环来处理许多图像并将它们布置在滚动视图中):

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

    这是我缺少的部分。将 scrollView 添加到单元格内容后,您需要使用 UIPageControl(起初对我来说这个实现并不明显)来设置实际的“视觉水平滚动”效果:

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

    希望对某人的搜索有所帮助 - 我在 Google 上花了很长时间寻找我刚刚解释过的示例,但除了对其工作原理的一般概述外,没有太多运气。

    【讨论】:

    • @user546459,我正在寻找类似的东西..我想水平滚动我的 uitableview 以便我可以阅读所有文本..我希望你知道如何解决这个问题..你能请检查我的问题:stackoverflow.com/questions/5337669/… --谢谢
    • 为了进一步澄清,我能够删除所有 tableView 实现,只需定义一个 UIScrollView 来为我完成工作。发现没有理由过度使用 tableView。
    猜你喜欢
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多