問題描述
我看到很多消息來源說可以在 UITableViewCell 中包含帶有 UIPageControl 的 UIScrollView 以便能夠水平滾動(通過可選圖像列表),但找不到任何可以滿足我要求的原始示例.我的滾動視圖工作"了,但我不確定如何繼續(xù) - 希望有人可以給我一些指導.
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)建了一個單元格,并將 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",我可以來回滾動,但顯然那里沒有內(nèi)容.如何使用可點擊"圖像列表填充 tableView/scrollView?任何方向都將不勝感激 - 最好不是嘿,這家伙做的有點相似,看看這個鏈接",但也許更多的是解釋如何正確實現(xiàn)這個功能(E特別是關(guān)于iOS 3.0+ - 據(jù)我了解,Apple 在實現(xiàn)這一點時讓我們的生活變得更輕松)
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)解決了自己的問題;也許沒有一次回答我的原因是因為一旦你了解了每個視圖的目的,它就是一個次要的實現(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)建了一個標準的 UITableViewCell
,但是我將單元格的框架更改為我自己的 1000 寬 x 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).
然后我創(chuàng)建了一個 UIScrollView
,將其設置為以下內(nèi)容(請記住,我在 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)建所需的圖像視圖(我意識到接下來我將創(chuàng)建一個循環(huán)來處理許多圖像并將它們布置在滾動視圖中):
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
(起初對我來說這個實現(xiàn)似乎并不明顯)來設置實際的視覺水平滾動"效果:
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.
這篇關(guān)于UITableView 中的 UIScrollView 和 UIPageControl的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!