Ios tableView:cellForRowAtIndexPath:返回null

Ios tableView:cellForRowAtIndexPath:返回null,ios,objective-c,uitableview,Ios,Objective C,Uitableview,在我的项目中,我有一个带有自定义单元格SearchCell的UITableView。但是,单元格返回为空值,将使应用程序崩溃。如果我选择捕获null异常并alloc/init一个新的单元格,它将显示为空。以下是代码: #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of se

在我的项目中,我有一个带有自定义单元格SearchCell的UITableView。但是,单元格返回为空值,将使应用程序崩溃。如果我选择捕获null异常并alloc/init一个新的单元格,它将显示为空。以下是代码:

    #pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    //return self.workingDatabase.count; This was causing an error for some reason, not too sure
    return 1; //using 1 just for debugging
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"InfoCell";
    SearchCell *cell = (SearchCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];

    /*
    if(cell == nil)
    {
        cell = [[SearchCell alloc] init];
    }*/

    // Configure the cell...
    Record *record = [self.workingDatabase objectAtIndex:indexPath.row];

    //set the content
    cell.myName.text = record.ABName;
    cell.myTarget.text = record.ABTarget;
    cell.myVendor.text = record.ABVendor;
    cell.myCatNumber.text = record.ABCatNumber;
    cell.myClonality.text = record.ABClonality;
    cell.myOrganism.text = record.ABSourceOrg;

    return cell;
}

问题是您的表视图使用的是静态单元格,而不是动态单元格(顺便说一句,这是一个非常重要的区别,在您发布的任何问题中都应该明确)。使用静态单元格时,不需要(通常也不应该)实现UITableView数据源方法。您可以在标签和表视图控制器本身之间建立出口连接,而不是到单元。事实上,您根本不需要自定义单元类(只需要这样您就可以连接动态单元的出口)。然后,您可以像在视图中填充任何其他标签一样填充标签。

您的
SearchCell
是全部在代码中完成的还是在IB中创建的自定义单元格?
SearchCell*cell=(SearchCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]那么您希望它返回一个SearchCell?请您显示代码或解释您正在执行的操作,以便实现这一点。您需要回答rmaddy的问题--您的问题的答案取决于您如何创建单元格和表视图。请检查这两件事。您是否将原型单元的类更改为SearchCell?它有标识符“InfoCell”吗?是的,标识符和类是对的,我想我使用的是动态单元格。至少我应该这样。我有这个记录数组,我将每个单元格显示为一个记录,整个tableview就是记录的集合。基于对数据库执行的搜索,它将改变,变大或变小。将它从静态更改为动态,现在它工作正常。我把它设置为静态,这样我就可以有一个粗略的大纲用户界面给教授看,但从来没有改变它回来。非常感谢你的帮助@NLDK,这不是IB中表视图所说的。只需将其更改为动态原型,然后我认为它应该可以工作。您还需要实现tableView:heightForRowAtIndexPath:以返回在IB中创建单元格的高度。@rdelmar我正在尝试从代码向单元格插入一个
UITextField
(因为当我从IB插入时,它无法调整大小以适应整个单元格)。我实现了
cellforrowatinexpath
方法来获取单元格的宽度和高度,但它们返回0。我不想硬编码
UITextField
的宽度和高度的值。有没有办法动态获取这些值?我发布了一个包含更多信息的帖子。