Horizontal UIScrollView inside a UITableViewCell

后端 未结 1 1890
旧巷少年郎
旧巷少年郎 2020-12-04 09:36

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

相关标签:
1条回答
  • 2020-12-04 10:23

    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.

    0 讨论(0)
提交回复
热议问题