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

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

问题描述

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

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

My问题是我可以滚动包含这个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
但它谈到旧的SDKs(即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;
}

感谢您的帮助!

推荐答案

我找到了一个解决方法。

I have found a workaround.

从头开始重新创建一个非常简单的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创建scrollView时,它有效吗?

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天全站免登陆