2013-08-18 14 views
1

內這是我的故事板:的UITableViewController細胞消失如果一個UIScrollView

Storyboard

我的滾動視圖是含有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 

一切都在第一次看呈現正常,但是任何姿勢/觸摸/ TAP我在的UITableView/ioBubbleList做,每一個細胞消失這樣的:

enter image description here enter image description here

如果從故事板從我做起導航控制器,然後一切正常。任何提示?

+0

您是否最終解決了這個問題? – enricmacias

回答

3

我解決了這個問題。

我的問題是我沒有保持UITableViewControllers和UITableViews我在UIScrollView內添加的引用。

我剛剛初始化和NSMutableArray並保存UITableViewControllers的方式,我把UITableViews添加到UIScrollView。

編輯

一些研究,我發現有一個更聰明的/更好的方式來做到這一點後。我們應該將視圖控制器作爲子視圖添加到容器視圖控制器(具有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,可能會發生一些錯誤。

相關問題