問(wèn)題描述
我看到很多消息來(lái)源說(shuō)可以在 UITableViewCell 中包含帶有 UIPageControl 的 UIScrollView 以便能夠水平滾動(dòng)(通過(guò)可選圖像列表),但找不到任何可以滿足我要求的原始示例.我的滾動(dòng)視圖工作"了,但我不確定如何繼續(xù) - 希望有人可以給我一些指導(dǎo).
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 中,我創(chuàng)建了一個(gè)單元格,并將 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(它會(huì)水平滾動(dòng),因?yàn)槲铱梢钥吹?scrollerIndicator 顯示了這一點(diǎn)),我將它的方法設(shè)置為以下:
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;
}
我的滾動(dòng)視圖將顯示horizo??ntalScroller",我可以來(lái)回滾動(dòng),但顯然那里沒(méi)有內(nèi)容.如何使用可點(diǎn)擊"圖像列表填充 tableView/scrollView?任何方向都將不勝感激 - 最好不是嘿,這家伙做的有點(diǎn)相似,看看這個(gè)鏈接",但也許更多的是解釋如何正確實(shí)現(xiàn)這個(gè)功能(E特別是關(guān)于iOS 3.0+ - 據(jù)我了解,Apple 在實(shí)現(xiàn)這一點(diǎn)時(shí)讓我們的生活變得更輕松)
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)
推薦答案
我已經(jīng)解決了自己的問(wèn)題;也許沒(méi)有一次回答我的原因是因?yàn)橐坏┠懔私饬嗣總€(gè)視圖的目的,它就是一個(gè)次要的實(shí)現(xiàn).
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:
內(nèi)我創(chuàng)建了一個(gè)標(biāo)準(zhǔn)的 UITableViewCell
,但是我將單元格的框架更改為我自己的 1000 寬 x 50 高的自定義框架(滿足我對(duì)項(xiàng)目的需要).
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).
然后我創(chuàng)建了一個(gè) UIScrollView
,將其設(shè)置為以下內(nèi)容(請(qǐng)記住,我在 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)];
然后我創(chuàng)建所需的圖像視圖(我意識(shí)到接下來(lái)我將創(chuàng)建一個(gè)循環(huán)來(lái)處理許多圖像并將它們布置在滾動(dòng)視圖中):
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 添加到單元格內(nèi)容后,您需要使用 UIPageControl
(起初對(duì)我來(lái)說(shuō)這個(gè)實(shí)現(xiàn)似乎并不明顯)來(lái)設(shè)置實(shí)際的視覺(jué)水平滾動(dòng)"效果:
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];
希望對(duì)某人的搜索有所幫助 - 我在 Google 上花了很長(zhǎng)時(shí)間尋找我剛剛解釋過(guò)的示例,但除了對(duì)其工作原理的一般概述之外沒(méi)有太多運(yùn)氣.
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.
這篇關(guān)于UITableView 中的 UIScrollView 和 UIPageControl的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!