1

这是我的故事板:

故事板

我的滚动视图包含 3 个 NavigationController(包含 BubbleListController,它是一种表视图)。这 3 个视图彼此放在一边,这是代码:

ioBubbleListController.h

#import <UIKit/UIKit.h>

@interface ioBubbleListController : UITableViewController {
    NSDictionary *bubbles;
}

@end

ioBubbleListController.m

#import "ioBubbleListController.h"

@interface ioBubbleListController ()
@property (nonatomic, retain) NSDictionary *bubbles;

@end

@implementation ioBubbleListController
@synthesize bubbles;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.bubbles = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bubbles" ofType:@"plist"]];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.bubbles count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.bubbles allKeys] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *continent = [self tableView:tableView titleForHeaderInSection:section];
    return [[self.bubbles valueForKey:continent] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CountryCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSString *country = [[self.bubbles valueForKey:continent] objectAtIndex:indexPath.row];

    cell.textLabel.text = country;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSString *country = [[self.bubbles valueForKey:continent] objectAtIndex:indexPath.row];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:[NSString stringWithFormat:@"You selected %@!", country]
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

这是我的ioUIPager将一个 BubbleList 放在另一个旁边

#import "ioUIPager.h"

@implementation ioUIPager

@synthesize scrollView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self scrollView].contentSize = CGSizeMake(self.scrollView.frame.size.width * 3, self.scrollView.frame.size.height);


    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];


    // View1
    UITableViewController *view1 = [sb instantiateViewControllerWithIdentifier:@"BubblesList"];

    CGRect frame1;
    frame1.origin.x = self.scrollView.frame.size.width * 0;
    frame1.origin.y = 0;
    frame1.size = self.scrollView.frame.size;

    [self.scrollView addSubview:view1.view];
    view1.view.frame = frame1;
    view1.view.backgroundColor = [UIColor greenColor];

    // View2
    UIViewController *view2 = [sb instantiateViewControllerWithIdentifier:@"BubblesList"];

    CGRect frame2;
    frame2.origin.x = self.scrollView.frame.size.width * 1;
    frame2.origin.y = 0;
    frame2.size = self.scrollView.frame.size;

    [self.scrollView addSubview:view2.view];
    view2.view.frame = frame2;
    view2.view.backgroundColor = [UIColor redColor];

    // View2
    UIViewController *view3 = [sb instantiateViewControllerWithIdentifier:@"BubblesList"];

    CGRect frame3;
    frame3.origin.x = self.scrollView.frame.size.width * 2;
    frame3.origin.y = 0;
    frame3.size = self.scrollView.frame.size;

    [self.scrollView addSubview:view3.view];
    view3.view.frame = frame3;


}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.scrollView = nil;
}

//- (void)dealloc {
//    [scrollView release];
//    [super dealloc];
//}

@end

乍一看,一切都很好,但是我在 UITableView/ioBubbleList 上所做的任何手势/触摸/点击,每个单元格都会像这样消失:

在此处输入图像描述 在此处输入图像描述

如果从故事板我从导航器控制器开始,那么一切正常。有什么提示吗?

4

1 回答 1

3

我解决了这个问题。

我的问题是我没有保留在 UIScrollView 中添加的 UITableViewControllers 和 UITableViews 的引用。

我刚刚初始化了 NSMutableArray 并以将 UITableViews 添加到 UIScrollView 中的方式保存了 UITableViewControllers。

编辑

经过一些研究,我发现有一种更聪明/更好的方法可以做到这一点。我们应该将视图控制器作为子视图添加到容器视图控制器(带有 UIScrollView 的 UIViewController),而不是将实例保存在我们自己的数组中。

这里来自苹果文档的解释: https ://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

这是我使用的代码:

self.addChildViewController(tableViewController) self.scrollView.addSubview(tableViewController.view) tableViewController.didMoveToParentViewController(self)

仅将 tableViewControllers 保存在数组中无法跟踪内容视图控制器内的层次结构。这意味着如果我们想从表视图控制器访问内容视图控制器,我们不能。如果我们使用它,UINavigationController 可能会发生一些错误。

于 2014-08-05T08:14:54.513 回答