为什么我的UITableView从UITableViewStyleGrouped更改为UITableViewStylePlain [英] Why does my UITableView change from UITableViewStyleGrouped to UITableViewStylePlain
问题描述
我的应用程序有一个扩展 UITableViewController
的视图控制器.初始化方法如下:
My application has a view controller that extends UITableViewController
. The initialization method looks like this:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithCoder:coder]) {
self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame
style:UITableViewStyleGrouped];
}
return self;
}
最初加载视图时,它显示为 UITableViewStyleGrouped
.但是,如果我的应用程序收到内存不足警告,则以上视图将更改为 UITableViewStylePlain
.没有与视图/控制器关联的xib文件.viewDidUnload和didReceiveMemoryWarning方法很简单:
When the view is initially loaded, it's displayed as UITableViewStyleGrouped
. However, if my app ever receives a low memory warning, the above view changes to UITableViewStylePlain
. There is no associated xib file with the View/Controller. The viewDidUnload and didReceiveMemoryWarning methods are straightforward:
- (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;
}
我的问题是,当我收到内存警告时,为什么表格样式会更改?
My question is, why does the table style change when I receive a memory warning?
它在设备和模拟器上发生.该设备是运行OS 3.1.3的3G iphone,模拟器正在运行OS 3.1
It happens on the device and on the simulator. The device is a 3G iphone running OS 3.1.3, the simulator is running OS 3.1
推荐答案
在初始化中,您调用 [super initWithCoder:coder]
.最好为 UITableViewController
覆盖指定的初始化程序,该初始化程序为 -initWithStyle:
.可能发生的情况是,当您通过调用 [super init ...]
创建表视图控制器时,它是在已设置其 tableView
属性的情况下创建的;也就是说,它将在初始化时创建表视图.这就是为什么您对 self.tableView.frame
的调用起作用的原因-如果 self.tableView
的值为 nil
,则该调用不起作用.这是一个更好的实现:
In your initialization, you call [super initWithCoder:coder]
. It would probably be better to override the designated initializer for UITableViewController
, which is -initWithStyle:
. What's probably happening is that when you create the table view controller by calling [super init…]
, it's being created with its tableView
property already being set; that is, it's creating the table view on initialization. That's why your call to self.tableView.frame
works—that shouldn't work if the value of self.tableView
is nil
. Here's a better implementation:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
}
return self;
}
这篇关于为什么我的UITableView从UITableViewStyleGrouped更改为UITableViewStylePlain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!