2012-07-28 79 views
2

我已經檢查了其他一些問題,但仍然無法弄清楚爲什麼會發生這種情況。我所知道的是在使用this的描述碼之後,單元格的值是null。我已經正確設置了tabledelegatedatasource,並且我看不到哪裏出了問題。iOS:UITableView dataSource必須從tableView返回一個單元格:cellForRowAtIndexPath:

如果我將以下方法的返回值設置爲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]]; 
    [email protected]"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,並將此項目與其他人進行了比較,以瞭解我是否遺漏了某些東西,但我看不到任何真正的差異。

回答

6

如果爲零從出列方法

變化

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

回到

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

良好,即固定它你是不是initisialing細胞。說明不再爲空,因此我現在可以轉到格式化單元格。謝謝,我會在一分鐘後接受答案。 – 2012-07-28 16:23:40

+0

哦?哇,不知道。謝謝,會閱讀它。 – 2012-07-28 16:34:15

+0

@Stavash不正確,會出現一個新的單元出列方法,但是當前的方法仍然存在 – wattson12 2012-07-28 18:01:09

1

您沒有方法返回第0部分中的行數 - 這是必需的數據源方法(在本例中)。

相關問題