如果在UIScrollView中,UITableViewController单元格将消失

时间:2013-08-18 11:05:25

标签: ios uitableview uiscrollview

这是我的故事板:

Storyboard

我的scrollView包含3个NavigationController(包含BubbleListController,它是一种Table视图)。这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上做的任何手势/触摸/点按,每个单元格都会像这样消失:

enter image description here enter image description here

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

1 个答案:

答案 0 :(得分:3)

我解决了这个问题。

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

我刚刚初始化了NSMutableArray并保存了UITableViewControllers,就像我将UITableViews添加到UIScrollView中一样。

修改

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

这里有来自apple docs的解释: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

这里是我使用的代码:

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

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