UISableViewCell内的水平UIScrollView(Horizontal UIScrollView inside a UITableViewCell)

编程入门 行业动态 更新时间:2024-10-10 05:23:22
UISableViewCell内的水平UIScrollView(Horizontal UIScrollView inside a UITableViewCell)

我试图创建与AppStore应用程序完全相同的效果,用于查看截图:在UITableView中,我有UITableViewCell的自定义子类。 其中一个旨在展示一些产品的预览,说4个图像。 我希望他们以与AppStore呈现应用程序截图相同的方式显示:在附带UIPageControl的水平UIScrollView中。

所以我添加我的UIScrollView和我的UIPageControl到我的UITableViewCell,就像这样:

@implementation phVolumePreviewCell - (id)init { if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kVolumePreviewCellIdentifier]) { [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; [self setSelectionStyle:UITableViewCellSeparatorStyleNone]; previews = [[NSMutableArray alloc] initWithCapacity:4]; for (int i = 0; i < 4; i++) { [previews addObject:@"dummy"]; } scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; [scrollView setPagingEnabled:YES]; [scrollView setBackgroundColor:[UIColor grayColor]]; [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack]; [scrollView setShowsHorizontalScrollIndicator:YES]; [scrollView setBounces:YES]; [scrollView setScrollEnabled:YES]; [scrollView setDelegate:self]; [self.contentView addSubview:scrollView]; [scrollView release]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero]; [pageControl setNumberOfPages:[previews count]]; [pageControl setBackgroundColor:[UIColor grayColor]]; [self.contentView addSubview:pageControl]; [pageControl release]; } return self; }

(注意:我正在使用虚拟数据填充“预览”,只是为了使[预览计数]有效;我想查看scrollView的滚动指示器仅用于测试目的,稍后会隐藏它们)。

我的问题是,我可以滚动包含这个phVolumePreviewCell(UITableViewCell的子类)的整个UITableView,但是我不能滚动UIScrollView。 我该如何实现?

我发现唯一的信息是: http : //theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html但它谈到旧的SDK(即2.2),我相信有更多的“最近”的做法。

一些精神:

我可以用两根手指滚动UIScrollView。 这不是我想要的,我希望它只能用一根手指滚动。 当我用两根手指滚动时,TableView仍然拦截触摸并滚动一点到顶部或底部。 当我触摸ScrollView时,我希望TableView坚持在其位置。

我相信我必须弄清楚如何拦截我的TableView触摸,如果触摸是在phVolumePreviewCell(自定义UITableViewCell子类),将其传递到单元格,而不是在TableView处理它。

为了记录,我的TableView是在UITableViewController子类中以编程方式创建的:

@interface phVolumeController : UITableViewController <UITableViewDelegate> { LocalLibrary *lib; NSString *volumeID; LLVolume *volume; } - (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID; @end

@implementation phVolumeController #pragma mark - #pragma mark Initialization - (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID { if (self = [super initWithStyle:UITableViewStylePlain]) { lib = [newLib retain]; volumeID = newVolumeID; volume = [(LLVolume *)[LLVolume alloc] initFromLibrary:lib forID:volumeID]; [[self navigationItem] setTitle:[volume title]]; } return self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier]; if (cell == nil) { cell = [[[phVolumePreviewCell alloc] init] autorelease]; } [(phVolumePreviewCell *)cell setVolume:volume]; return (UITableViewCell *)cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return kVolumePreviewCellHeight; }

感谢任何帮助!

I'm trying to create the exact same effect as in the AppStore app for viewing screenshots: inside a UITableView, I have custom subclasses of UITableViewCell's. One of them is designed to show previews of some product, say 4 images. I want them to show the same way as the AppStore presents screenshots of an app : inside a horizontal UIScrollView with its attached UIPageControl.

So I add my UIScrollView and my UIPageControl to my UITableViewCell, just like that :

@implementation phVolumePreviewCell - (id)init { if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kVolumePreviewCellIdentifier]) { [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; [self setSelectionStyle:UITableViewCellSeparatorStyleNone]; previews = [[NSMutableArray alloc] initWithCapacity:4]; for (int i = 0; i < 4; i++) { [previews addObject:@"dummy"]; } scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; [scrollView setPagingEnabled:YES]; [scrollView setBackgroundColor:[UIColor grayColor]]; [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack]; [scrollView setShowsHorizontalScrollIndicator:YES]; [scrollView setBounces:YES]; [scrollView setScrollEnabled:YES]; [scrollView setDelegate:self]; [self.contentView addSubview:scrollView]; [scrollView release]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero]; [pageControl setNumberOfPages:[previews count]]; [pageControl setBackgroundColor:[UIColor grayColor]]; [self.contentView addSubview:pageControl]; [pageControl release]; } return self; }

(note: I'm populating "previews" with dummy data here, just in order to have a [previews count] that works ; I want to view the scrollView's scroll indicator for test purposes only, I'll hide them later on).

My problem is that I can scroll the whole UITableView containing this phVolumePreviewCell (subclass of UITableViewCell), but I can't scroll the UIScrollView. How can I achieve this?

The only information I found is there: http://theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html But it talks of old SDKs (namely 2.2) and I'm sure there is a more "recent" approach to doing this.

Some precisions :

I can scroll the UIScrollView with two fingers. That's not what I want, I want it to be able to scroll with one finger only. When I scroll with two fingers, the TableView still intercepts the touches and scroll a little bit to the top or the bottom. I'd like the TableView to stick at its position when I touch the ScrollView.

I believe I have to work out how to intercept touches on my TableView, and if the touch is on the phVolumePreviewCell (custom UITableViewCell subclass), pass it to the cell instead of handling it on the TableView.

For the record, my TableView is created programmatically in a UITableViewController subclass:

@interface phVolumeController : UITableViewController <UITableViewDelegate> { LocalLibrary *lib; NSString *volumeID; LLVolume *volume; } - (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID; @end

 

@implementation phVolumeController #pragma mark - #pragma mark Initialization - (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID { if (self = [super initWithStyle:UITableViewStylePlain]) { lib = [newLib retain]; volumeID = newVolumeID; volume = [(LLVolume *)[LLVolume alloc] initFromLibrary:lib forID:volumeID]; [[self navigationItem] setTitle:[volume title]]; } return self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier]; if (cell == nil) { cell = [[[phVolumePreviewCell alloc] init] autorelease]; } [(phVolumePreviewCell *)cell setVolume:volume]; return (UITableViewCell *)cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return kVolumePreviewCellHeight; }

Thanks for any help!

最满意答案

我找到了一个解决方法。

从头开始重新创建一个非常简单的UITableView(8行),并在第二行插入UIScrollView,工作完美。 嵌套滚动视图(TableView和ScrollView)按预期方式独立滚动。 在AppDelegate中的单文件测试项目中进行的所有操作。

所以...首先我以为“这可能是一个代理问题”,所以我告诉scrollView它的委托是TableView,而不是我的自定义TableViewCell子类。 无济于事

然后,我不是使用一个自定义的,干净的TableViewCell子类,它只是添加了ScrollView和PageControl,而是在TableView的cellForRowAtIndexPath:方法中创建了一个简单的TableViewCell。 然后,我在初始化TableViewCell之后立即创建一个UIScrollView,然后将其添加到TableViewCell,并且shazam ...它的工作原理!

之前:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier]; if (cell == nil) { cell = [[[phVolumePreviewCell alloc] init] autorelease]; } [(phVolumePreviewCell *)cell setVolume:volume]; // That does the job of creating the cell's scrollView and pageControl return (UITableViewCell *)cell

后:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kvcPreviewRowIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kvcPreviewRowIdentifier] autorelease]; previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kvcPreviewScrollViewHeight)]; [previewScrollView setContentSize:CGSizeMake(1000, kvcPreviewScrollViewHeight)]; [[cell contentView] addSubview:previewScrollView]; previewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kvcPreviewScrollViewHeight, tableView.frame.size.width, kvcPreviewPageControlHeight)]; [previewPageControl setNumberOfPages:4]; [[cell contentView] addSubview:previewPageControl]; } return (UITableViewCell *)cell;

当我从TVCell子类创建我的scrollView时,它不会很好玩, 但是当我创建一个“古典”TVCell之后,我从TableView创建了scrollView,它可以运行吗?

我可能找到了一个解决方案,但我仍然对此感到困惑。

I have found a workaround.

Recreating from scratch a very simple UITableView (8 rows), and inserting a UIScrollView in just the second row, works perfectly. Nested scroll views (the TableView and the ScrollView) scroll independently as expected. All that was done in a single-file test project, in the AppDelegate.

So... first I thought "this might be a delegation problem", so I told the scrollView that its delegate was the TableView, not my custom TableViewCell subclass. To no avail.

Then instead of resorting to a custom, clean TableViewCell subclass that just adds the ScrollView and the PageControl, I resorted to creating a plain TableViewCell in the TableView's cellForRowAtIndexPath: method. Then, I create a UIScrollView right after initializing the TableViewCell, then add it to the TableViewCell, and shazam... it works!

Before:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier]; if (cell == nil) { cell = [[[phVolumePreviewCell alloc] init] autorelease]; } [(phVolumePreviewCell *)cell setVolume:volume]; // That does the job of creating the cell's scrollView and pageControl return (UITableViewCell *)cell

After:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kvcPreviewRowIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kvcPreviewRowIdentifier] autorelease]; previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kvcPreviewScrollViewHeight)]; [previewScrollView setContentSize:CGSizeMake(1000, kvcPreviewScrollViewHeight)]; [[cell contentView] addSubview:previewScrollView]; previewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kvcPreviewScrollViewHeight, tableView.frame.size.width, kvcPreviewPageControlHeight)]; [previewPageControl setNumberOfPages:4]; [[cell contentView] addSubview:previewPageControl]; } return (UITableViewCell *)cell;

How the heck comes that when I create my scrollView from the TVCell subclass, it doesn't play nice ; but when I create the scrollView from the TableView right after creating a "classical" TVCell, it works?

I might have found a solution, but I'm still puzzled by the reason.

更多推荐

本文发布于:2023-07-30 15:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水平   UISableViewCell   UIScrollView   UITableViewCell   Horizontal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!