UITableView 中的 UIScrollView 和 UIPageControl
我看到很多消息来源说可以在 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;
}
我的滚动视图将指示“horizontalScroller”,我能够来回滚动,但显然那里没有内容。 如何使用“可点击”图像列表填充 tableView/scrollView?任何方向将不胜感激 - 最好不是“嘿,这家伙做了一些类似的事情,请查看此链接”,但也许更多的是如何正确实现此功能的解释(E特别是关于iOS 3.0+ - 据我了解,苹果通过实现这一点让我们的生活变得更轻松)
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.
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
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;
}
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了我自己的问题;也许没有人回答我的原因是因为一旦你理解了每个视图的目的,它就是一个小的实现。
从
cellForRowAtIndexPath:
中我创建了一个标准的
UITableViewCell
,但是我将单元格的框架更改为我自己的自定义框架,宽度为 1000 x 高度为 50(满足我的项目需求)。然后,我创建了一个 UIScrollView,将其设置为以下内容(请记住,我在 IB 中定义了 tableView,因此我将一些高度/宽度映射到这些值):
然后我创建所需的图像视图(我意识到接下来我将创建一个循环来处理许多图像并将它们放置在滚动视图上):
这是我缺少的部分。将scrollView添加到单元格内容后,您需要使用
UIPageControl
(对于这个实现,一开始对我来说这似乎并不明显)来设置实际的“视觉水平滚动”效果:希望帮助某人的搜索 - 我花了相当长的时间在谷歌上寻找我刚刚解释的例子,除了它如何工作的一般概述之外没有太多运气。
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.
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).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):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):
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: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.