我检查了其他几个问题,但仍然无法弄清楚为什么会这样。我所知道的是在使用来自 this 的描述代码之后质疑单元格的值为 null
。我已经正确设置了 table
的 delegate
和 datasource
,但我看不出哪里出了问题。
如果我将以下方法的返回值设置为 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/