2

我检查了其他一些问题,但仍然无法弄清楚为什么会发生这种情况。我所知道的是,在使用问题中的描述代码后,单元格的值为null. 我已经正确设置了delegatedatasource,但table我看不出哪里出了问题。

如果我将以下方法的返回值设置为 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过这个项目,并将这个项目与其他项目进行了比较,看看我是否遗漏了一些东西,但我看不出任何真正的差异。

4

2 回答 2

6

如果从 dequeue 方法返回 nil,则您没有初始化单元格

改变

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
于 2012-07-28T16:19:30.740 回答
1

您没有返回第 0 部分中的行数的方法 - 这是必需的数据源方法(在这种情况下)。

于 2012-07-28T16:14:13.017 回答