iOS:UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:

Mo *_*osa 2 datasource tableview ios

我已经检查了一些其他问题,但仍然无法弄清楚为什么会发生这种情况.我所知道的是在使用问题的描述代码后,单元格的值是null.我给自己定的delegatedatasourcetable正确,我看不出这是怎么回事错误.

如果我将以下方法的返回值设置为0,则不会发生错误,因为该cellForRowAtIndexPath方法实际上并未发生.因此,如果我将其设置为1(应该是),那么它将抛出错误.我已经合成NSArray并检查了它的填充(虽然这应该不重要我猜)并且基本上意味着发生的是我按下button搜索然后将结果放入table.所以在此之前tableview是空的.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题.我以前用过tableviews,并将这个项目与其他人比较,看看我是否错过了什么,但我看不出任何真正的差异.

wat*_*n12 6

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

更改

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)