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

initWithStyle:(UITableViewStyle)样式看起来未调用,uitableview,initwithstyle,Uitableview,Initwithstyle,在其.m的默认init方法中创建UITableViewController的默认视图控制器,如下所示 - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:UITableViewStyleGrouped]; if (self) { // Custom initialization } return self; } 在添加数据源和委托方法的标准过程之后,请更正iPhone

在其
.m
的默认init方法中创建
UITableViewController
的默认视图控制器,如下所示

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    // Custom initialization
    }
return self;
}
在添加数据源和委托方法的标准过程之后,请更正iPhone模拟器上显示的表视图

我的问题是,当试图添加
NSLog(@“did init”)
中,如果
,如下所示

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
    // Custom initialization
    NSLog(@"did init");
}
return self;
}
但再次运行时,
没有在控制台上显示init
。即使在
之前或之后移动
NSLog
,如果
,两者都不起作用

有什么问题吗?为什么
initWithStyle:(UITableViewStyle)样式不起作用

如果按照Michael的建议使用
initWithCoder

- (id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"init?");
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
    NSLog(@"init done");
}
return self;
}
init?
init done
工作并显示在控制台上。但这也是一次失败

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
实际上,如果删除`initWithCoder,应用程序就可以了

cellforrowatinexpath
处的代码如下:

- (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:
”是从代码创建UITableViewController的方法

如果要从情节提要创建对象,则真正调用的方法是:

- (id)initWithCoder:(NSCoder *)decoder
您还将收到一条“
awakeFromNib
”消息

编辑以添加:

如果您试图使用“
initWithCoder
”方法执行某些操作,请同时调用“
super
”,如下所示:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"init?");
    self = [super initWithCoder: aDecoder];
    if (self) {
        NSLog(@"init done");
    }
    return self;
}
initWithStyle:
”是从代码创建UITableViewController的方法

如果要从情节提要创建对象,则真正调用的方法是:

- (id)initWithCoder:(NSCoder *)decoder
您还将收到一条“
awakeFromNib
”消息

编辑以添加:

如果您试图使用“
initWithCoder
”方法执行某些操作,请同时调用“
super
”,如下所示:

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

在调用“
initWithStyle
”以设置表视图控制器的位置显示代码?或者您是否将此表视图控制器放入了XIB或故事板中?在MainstryBoard中,只有一个表视图控制器嵌入到导航控制器中。因此
initWithStyle
在此表视图控制器的
.m
文件中。是否显示调用“
initWithStyle
”以设置表视图控制器的代码?或者您是否将此表视图控制器放入了XIB或故事板中?在MainstryBoard中,只有一个表视图控制器嵌入到导航控制器中。所以
initWithStyle
在这个表视图控制器的
.m
文件中。请参阅我问题的补充。我做了。希望这能解决你的问题。请看我问题的补充。我做到了。希望这能解决你的问题。