ios - TableView :cellForRowAtIndexPath: returning null

标签 ios objective-c uitableview

在我的项目中,我有一个带有自定义单元格 SearchCell 的 UITableView。然而,单元格返回为空值,并且会使应用程序崩溃。如果我选择捕获 null 异常并分配/初始化一个新单元格,它会显示为空白。以下是代码:

    #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;
}

最佳答案

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

关于ios - TableView :cellForRowAtIndexPath: returning null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391333/

相关文章:

ios - 在显示表格之前加载解析中的所有数据

ios - 点击 Swift 时全屏显示 tableviewcell 中的图像

ios - 应用程序在 iOS6 中运行良好,在 iOS7 中运行缓慢且不稳定

ios - UIAlertView 按钮数组

Objective-C:什么是私有(private)的什么不是?

ios - UITableView部分中的多种单元格类型

ios - UICollectionViewCell 的 SelectedBackgroundView 在不应该可见时可见

ios - Google 跟踪代码管理器未在 Ios 中发送事件

ios - 是否可以增加 UITableView 分隔线的粗细?

iOS 15 : Remove empty space before cells in UITableView