iOS:UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回一个单元格:

标签 ios datasource tableview

我检查了其他几个问题,但仍然无法弄清楚为什么会这样。我所知道的是在使用来自 this 的描述代码之后质疑单元格的值为 null。我已经正确设置了 tabledelegatedatasource,但我看不出哪里出了问题。

如果我将以下方法的返回值设置为 0,则不会发生错误,因为 cellForRowAtIndexPath 方法并没有真正发生。因此,如果我将其设置为 1(应该如此),它就会抛出错误。我已经合成了 NSArray 并检查了它是否已填充(虽然我猜这应该无关紧要)并且基本上我想要发生的是我按下一个 button 来搜索和然后将结果放在 table 中。所以在此之前 tableview 是空的。

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

这是 cellForRowAtIndexPath 方法本身。我也试过只使用基本的模板方法,它仍然会抛出同样的错误。

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

    // Configure the cell...

    NSDictionary *aResult = [results objectAtIndex:[indexPath row]];
    NSLog(@"RESULTS IN TABLE %i", [results count ]);


    id key = [results objectAtIndex:[indexPath row]];
    resultTitle.text=@"Hello";

    NSURL *url = [NSURL URLWithString:[aResult objectForKey:@"url"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    cell.imageView.image = [UIImage imageWithData:data];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [theTableView reloadData];

    return cell;
}

我不知道哪里出了问题。我以前使用过 tableviews 并将这个项目与其他项目进行比较,看看我是否遗漏了什么,但我看不出任何真正的区别。

最佳答案

如果从 dequeue 方法返回 nil,则不会初始化单元格

改变

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

关于iOS:UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回一个单元格:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702603/

相关文章:

ios - 如何在同一个 View Controller 中使用 2 个 UIPickerViews?

ios - Swift TableView 单元格播放按钮点击和其他单元格按钮需要重置

swift - 在表/ Collection View 之外循环核心数据对象

tableview - 如何填充在 JavaFx Scene Builder 中设计的 fxml 文件中定义的 TableView

ios - 如何在 objective-c 中创建 json

ios - 找不到 Facebook sdk 3.9 文件

java - 创建用于屏幕传输 RTSP 的处理器

java - 如何使用 CLI 在 Wildfly 中设置 MariaDB 数据源

ios - UITextfield 的文本在填充后不会向左移动 - 因为我一直在输入

javascript - 在移动设备上循环 <audio>