2010-12-01 15 views
36

私はスクリーンショットを表示するためにAppStoreアプリケーションとまったく同じエフェクトを作成しようとしています.UITableView内には、UITableViewCellのカスタムサブクラスがあります。そのうちの1つは、いくつかの製品、例えば4つの画像のプレビューを表示するように設計されています。 AppStoreがアプリのスクリーンショットを表示するのと同じ方法で表示するようにします。水平なUIScrollViewの内部に、UIPageControlが添付されています。UITableViewCell内の水平UIScrollView

だから私はちょうどそのように、私のUITableViewCellに私UIScrollViewの、私のUIPageControlを追加します。

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

は(注:私はちょうど[プレビューを持つために、ここではダミーデータで「プレビュー」を投入していますcount]は動作しています;私はscrollViewのスクロールインジケータをテスト目的でのみ表示したいので、後で非表示にします)。

私の問題は、このphVolumePreviewCell(UITableViewCellのサブクラス)を含むUITableView全体をスクロールすることができますが、私はUIScrollViewをスクロールできません。どうすればこれを達成できますか?

私が見つけた唯一の情報は次のとおりです:http://theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html 古いSDK(すなわち2.2)について話していますが、これを行うための「最近の」アプローチがあると確信しています。

いくつかの精度:

  • 私は2本の指でUIScrollViewのをスクロールすることができます。それは私が欲しいものではない、私はそれが1本の指でのみスクロールできるようにしたい。
  • 2本の指でスクロールすると、TableViewはまだタッチを傍受し、少し上または下にスクロールします。 ScrollViewに触れるとTableViewをその位置に固定したいと思います。

は、私は私のtableViewにタッチを傍受する方法を動作するように持っていると信じて、そしてタッチがphVolumePreviewCell(カスタムUITableViewCellのサブクラス)にある場合、テーブルビューでそれを処理するのではなく、セルにそれを渡します。レコードの

は、私のテーブルビューはの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; 
} 

感謝を!

答えて

21

解決方法が見つかりました。

非常に単純なUITableView(8行)を最初から再作成し、2番目の行にUIScrollViewを挿入すると完全に機能します。ネストされたスクロールビュー(TableViewとScrollView)は、期待どおりに独立してスクロールします。 AppDelegateの単一ファイルテストプロジェクトで行われたことすべて。

だから私は "これは委任の問題かもしれない"と思ったので、その私のカスタムTableViewCellサブクラスではなく、そのデリゲートがTableViewであることをscrollViewに伝えました。無駄に。

次に、ScrollViewとPageControlだけを追加するカスタムのクリーンなTableViewCellサブクラスに頼る代わりに、私は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 

後:

一体私はTVCellサブクラスからの私scrollViewを作成するとき、それは素晴らしいプレーしないことを来てどのように
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を作成した直後に、TableViewからscrollViewを作成すると動作しますか?

私は解決策を見つけたかもしれませんが、私はまだその理由に困惑しています。

+0

Thanks Cyrille !!同じ問題がここにあります。あなたの解決策は私に絶望の時間の多くを救った! :-) –

+0

こんにちはCyrille - これは古いSOの質問ですが、このソリューションのサンプルアプリを持っているのだろうかと思いました。ありがとう.. – Craig

+0

@Craig:いいえサンプルアプリ、申し訳ありません。このコードはそれ以来進化しており、私はもうこれを使用しません。しかし、本当に、この状況を再現するために必要なコードはすべて私の記事にあります。 – Cyrille

関連する問題