1

これは私のストーリーボードです:

絵コンテ

私のscrollViewには、3つのNavigationController(一種のTableビューである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で、1 つの BubbleList を別の 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 の参照を保持していなかったことです。

UITableViews を UIScrollView に追加した方法で、UITableViewControllers を初期化し、NSMutableArray を保存しました。

編集

いくつかの調査の後、これを行うためのよりスマートでより良い方法があることがわかりました。インスタンスを独自の配列内に保持する代わりに、ビュー コントローラーを子としてコンテナー ビュー コントローラー (UIScrollView を持つ UIViewController) に追加する必要があります。

Apple ドキュメントからの説明: 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 に答える