UITableViewCell 内的水平 UIScrollView [英] Horizontal UIScrollView inside a UITableViewCell

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

问题描述

我正在尝试创建与 AppStore 应用程序中查看屏幕截图完全相同的效果:在 UITableView 中,我有 UITableViewCell 的自定义子类.其中之一旨在显示某些产品的预览,例如 4 张图像.我希望它们的显示方式与 AppStore 显示应用程序屏幕截图的方式相同:在带有附加 UIPageControl 的水平 UIScrollView 中.

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.

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

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

(注意:我在这里用虚拟数据填充预览",只是为了让 [预览计数] 有效;我想查看 scrollView 的滚动指示器仅用于测试目的,稍后我将隐藏它们上).

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

我的问题是我可以滚动包含这个 phVolumePreviewCell(UITableViewCell 的子类)的整个 UITableView,但是我不能滚动 UIScrollView.我怎样才能做到这一点?

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?

我找到的唯一信息是:http://theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html但它谈到了旧的 SDK(即 2.2),我相信有一种更最近"的方法来做到这一点.

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.

一些精度:

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

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

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.

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

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

感谢您的帮助!

推荐答案

我找到了解决方法.

从头开始重新创建一个非常简单的 UITableView(8 行),并在第二行插入一个 UIScrollView,效果很好.嵌套滚动视图(TableView 和 ScrollView)按预期独立滚动.所有这些都是在 AppDelegate 中的单文件测试项目中完成的.

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.

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

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.

然后,我没有求助于只添加 ScrollView 和 PageControl 的自定义、干净的 TableViewCell 子类,而是求助于在 TableView 的 cellForRowAtIndexPath: 方法中创建一个普通的 TableViewCell.然后,我在初始化 TableViewCell 后立即创建一个 UIScrollView,然后将其添加到 TableViewCell 中,然后 shazam ......它起作用了!

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!

之前:

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 创建滚动视图时,它是否有效?

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.

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

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