内存警告后,自定义UITableView headerView消失

psy*_*tik 15 iphone cocoa-touch uitableview ios

我有一个UITableViewController.我tableViewloadView方法中创建了一个自定义headerView,如下所示:

(void)loadView {
    [super loadView];

    UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height * 2 )];  
    containerView.tag = 'cont';
    containerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(padding, height, width, height);
    ... //configure UIButton and events

    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"] highlightedImage:[UIImage imageNamed:@"highlight.png"]];    
    imageView.frame = CGRectMake(0, 0, width, height );
    ... //configure UIImageView

    [containerView addSubview:button];
    [containerView addSubview:imageView];
    self.tableView.tableHeaderView = containerView;

    [imageView release];
    [containerView release];
}
Run Code Online (Sandbox Code Playgroud)

没有其他方法(viewDidLoad/Unload等)被重载.

该控制器托管在一个选项卡中.当我切换到另一个选项卡并模拟内存警告,然后回到此选项卡时,我UITableView错过了我的custon标题.正如我所料,所有行/部分都是可见的.将BP放在loadView上面的代码中,我看到在内存警告后切换回选项卡时调用它,但我实际上看不到标题.

关于我在这里缺少什么的想法?

编辑:这发生在设备和模拟器上.在设备上,我只是通过在我的后台打开一堆不同的应用程序时发出内存警告.

小智 6

loadView方法不应该调用super.尝试删除[super loadView]; 这可能很好地解决了你的问题.其他重写的视图方法(viewDidLoad,viewWillAppear等)可以调用super就好了.请参阅以下文档:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

确认.


小智 2

保留containView而不是释放它。并在 tableView:viewForHeaderInSection: (UITableViewDelegate 方法) 中返回它。