简体   繁体   English

initWithStyle:(UITableViewStyle)样式看起来未调用

[英]initWithStyle:(UITableViewStyle)style looks not called

create a default view controller of UITableViewController , in its .m , default init method as below 创建UITableViewController的默认视图控制器,其.m默认的init方法,如下所示

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    // Custom initialization
    }
return self;
}

after standard procedure to add data source and delegate methods, correct table view shown on iPhone simulator. 在添加数据源和委托方法的标准过程之后,请更正iPhone模拟器上显示的表格视图。

my question is, when trying to add NSLog(@"did init"); 我的问题是,当尝试添加NSLog(@"did init"); in if , like below if ,如下所示

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
    // Custom initialization
    NSLog(@"did init");
}
return self;
}

but run again, did init not show on console. 但再次运行, did init未显示在控制台上。 even move NSLog before or after if , neither doesn't work! 甚至在if之前或之后移动NSLog ,都不行!

What's the problem? 有什么问题? Why initWithStyle:(UITableViewStyle)style not work? 为什么initWithStyle:(UITableViewStyle)style不起作用?

if using initWithCoder as suggested by Michael 如果按照迈克尔的建议使用initWithCoder

- (id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"init?");
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
    NSLog(@"init done");
}
return self;
}

init? and init done worked and show on console. 并且init done工作并在控制台上显示。 But a failure as well. 但是也是失败的。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Actually, if deleting `initWithCoder, app is ok. 实际上,如果删除`initWithCoder,则应用程序正常。

Codes at cellForRowAtIndexPath as following, cellForRowAtIndexPath代码如下,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

NSArray * array = [[temp objectAtIndex:indexPath.section] valueForKey:@"English"];
cell.textLabel.text = [array objectAtIndex:indexPath.row];

if (indexPath.row == 0) {
    cell.textLabel.textColor = [UIColor blueColor];
}

return cell;

" initWithStyle: " is what one calls creating a UITableViewController from code. initWithStyle: ”是从代码创建UITableViewController的调用。

If you're creating the object from a storyboard, the method that really gets called is this: 如果要从情节提要中创建对象,则真正调用的方法是:

- (id)initWithCoder:(NSCoder *)decoder

You'll also get an " awakeFromNib " message too. 您还将收到“ awakeFromNib ”消息。

EDITED to add: 编辑添加:

If you're trying to do something with your " initWithCoder " method, call " super " as well, like this: 如果您尝试使用“ initWithCoder ”方法执行某项操作,请同样调用“ super ”,如下所示:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"init?");
    self = [super initWithCoder: aDecoder];
    if (self) {
        NSLog(@"init done");
    }
    return self;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM