UITableView中的UIScrollView和UIPageControl [英] UIScrollView and UIPageControl within UITableView

查看:66
本文介绍了UITableView中的UIScrollView和UIPageControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多消息来源说,有可能在UITableViewCell内包含带有UIPageControl的UIScrollView ,以便能够水平滚动(通过可选图像列表) ),但找不到能满足我需求的任何原始示例.我的滚动视图正常运行",但我不确定该如何前进-希望有人可以给我一些指导.

I've seen lots of sources saying it is possible to include a UIScrollView with UIPageControl inside a UITableViewCell to be able to scroll horizontally (through a list of selectable images), but can't find any raw examples that does what I want. I've gotten my scroll view "working", but I am unsure how to go forward - hopefully someone can send me some guidance.

在cellForRowAtIndexPath内,我创建一个单元格,并将scrollView和pageControl都添加为子视图.

Within my cellForRowAtIndexPath, I create a cell, and add both a scrollView and pageControl as subviews.

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

我已经附上了所显示内容的屏幕截图

I've attached a screenshot of what's being displayed

主视图的底部是我的UITableView,其中包含scrollView/pageControl(它将水平滚动,如我所见的scrollerIndicator所示),并且将其方法设置为以下内容:

the bottom portion of the main view is my UITableView that contains the scrollView/pageControl (and it will scroll horizontally, as I can see the scrollerIndicator showing this), and I've got its method's set to the following:

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

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

我的滚动视图将显示一个" horizo​​ntalScroller ",并且我能够来回滚动,但是显然那里没有 . 如何使用可点击"图片列表填充tableView/scrollView?任何方向都将不胜感激-最好不要嘿,这个家伙做了一些类似的操作,请查看此链接",但可能更多地说明了应如何正确实现此功能(E 特别是关于iOS 3.0以上版本-据我了解,Apple在实现此目标方面让我们的生活变得更加轻松)

My Scroll view will indicate a "horizontalScroller" and I am able to scroll back and forth, but obviously there's no content there. How do I go about populating the tableView/scrollView with say, a list of "clickable" images? Any direction would be greatly appreciated - preferably not a "hey this guy's done it somewhat similar, check out this link" but maybe more an explanation of how this functionality should be implemented correctly (ESPECIALLY in regards to iOS 3.0+ - it is my understanding Apple has made our lives easier in implementing this)

推荐答案

我已经解决了自己的问题;也许没有人回答我的原因是因为一旦您了解了每个视图的目的,它只是一个次要的实现.

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.

cellForRowAtIndexPath:内部 我创建了标准的UITableViewCell,但是我将单元格的框架更改为自己的自定义框架,宽度为1000宽度乘以50高度(根据我对项目的需求).

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).

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

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

这是我所缺少的部分.在将scrollView添加到单元格内容中之后,您需要使用UIPageControl(对于本实现起初我似乎并不明显)来设置实际的视觉水平滚动"效果:

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

希望对某人的搜索有所帮助-我在Google上花费了相当多的时间来寻找我刚才解释的示例,除了对其工作方式的总体概述以外,没有多少运气.

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.

这篇关于UITableView中的UIScrollView和UIPageControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆