iphone - 为什么我的 UITableView 从 UITableViewStyleGrouped 更改为 UITableViewStylePlain

标签 iphone objective-c

我的应用程序有一个扩展UITableViewController的 View Controller 。初始化方法如下所示:

- (id)initWithCoder:(NSCoder*)coder {
    if (self = [super initWithCoder:coder]) {
        self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame 
                                                      style:UITableViewStyleGrouped];
    }

    return self;
}

当 View 最初加载时,它显示为UITableViewStyleGrouped。但是,如果我的应用收到内存不足警告,上述 View 将更改为 UITableViewStylePlain。没有与 View / Controller 关联的 xib 文件。 viewDidUnload 和 didReceiveMemoryWarning 方法很简单:

- (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;
}

我的问题是,为什么当我收到内存警告时表格样式会发生变化?

它发生在设备和模拟器上。该设备是运行 OS 3.1.3 的 3G iphone,模拟器运行 OS 3.1

最佳答案

在初始化中,您调用 [super initWithCoder:coder] 。覆盖 UITableViewController 的指定初始化程序可能会更好。 ,即-initWithStyle: 。可能发生的情况是,当您通过调用 [super init…] 创建 TableView Controller 时,它是用 tableView 创建的属性已被设置;也就是说,它在初始化时创建 TableView 。这就是您调用 self.tableView.frame 的原因有效 - 如果 self.tableView 的值不应该有效是 nil 。这是一个更好的实现:

- (id)initWithCoder:(NSCoder*)coder {
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {

    }

    return self;
}

关于iphone - 为什么我的 UITableView 从 UITableViewStyleGrouped 更改为 UITableViewStylePlain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2639340/

相关文章:

iphone - 响应 NSNotifications 时,更新 UIViews 的最佳实践是什么

objective-c - 如何将宏和常量放入 iOS 项目的 .pch 文件中?

ios - 什么时候不为 objective-c 中的委托(delegate)使用协议(protocol)?

iphone - 动态访问属性

ios - 原型(prototype)可重用 tableview 单元格渲染 subview 很差

iphone - 屏幕锁定时如何关闭我的 iPhone 应用程序?

iphone - 在 "off"状态改变 UISwitch 的颜色

ios - 如何在不同的 View Controller 中保留指向对象的指针

ios - 如何在字典中设置文本字段的值?