Ios 情节提要中的UITableviewCell未初始化

Ios 情节提要中的UITableviewCell未初始化,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想创建带有故事板的UITableView,如图所示: ITViewController.m中的委托方法如下: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } -

我想创建带有故事板的UITableView,如图所示:

ITViewController.m中的委托方法如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

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

    return cell;
}
当我运行我的应用程序时,我得到了错误:

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

似乎没有单元已初始化,如何解决此问题?太棒了

您可以这样初始化单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"ITTableViewCell";       

     UTTableViewCell *cell = (UTTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
     }  

     return cell;
}

单元的标识符必须是故事板中的ITTableViewCell。故事板使其非常简单-您只需检查故事板中为ITTableViewCell指定的重用标识符是否为@ITTableViewCell。@rajesh我已通过设置标识符解决了此问题。我没有看到identifier属性,因为不知何故隐藏了tableviewcell的属性列表。太棒了!如果单元是在故事板中生成的,那么就不需要有ifcell==nil子句——如果您使用正确的标识符,它将永远不会是nil。是的,这是一种老方法,您不再需要这样做