2011-05-18 44 views
0

我在那裏我是從不同的筆尖加載自定義單元格的表視圖分配的標籤值,我得到異常:例外,而在自定義單元格

 2011-05-18 18:01:00.323 custInfoService[4370:20b] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:4709 
2011-05-18 18:01:00.324 custInfoService[4370:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 
2011-05-18 18:01:00.325 custInfoService[4370:20b] Stack: (
    11125851, 
    2442997307, 

當我用的斷點,然後我來認識xception是在開關case.I無法弄清楚爲什麼它給予異常?幫我! 這裏是我的代碼:

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"BookDetailViewController" owner:self options:NULL]; 
     cell = nibloadedcell; 
     } 

    tbcel.layer.cornerRadius = 10; 
    tbcel.text = aBook.Name; 


    UILabel *fieldlabel = (UILabel *) [cell viewWithTag:1]; 
    fieldlabel.text = [fieldarray objectAtIndex:(indexPath.row)]; 
    UILabel *valuelabel = (UILabel *) [cell viewWithTag:2]; 
    switch(indexPath.section) 

    { 
     case 0: 
      valuelabel.text = aBook.Address; 
      break; 
     case 1: 
      valuelabel.text = aBook.Phone; 
      break; 
     case 2: 
      valuelabel.text = aBook.Purchase; 
    } 

    return cell; 
} 

回答

0

所以最明顯的問題是

[[NSBundle mainBundle] loadNibNamed:@"BookDetailViewController" owner:self options:NULL]; 
cell = nibloadedcell; 

沒有設置你的nibloadedcell出口,所以cellnil一路過關斬將。因此沒有有效的單元格被返回,並且您收到錯誤UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

不要忘記,消息nil默默無視 - 如果單元格是nil,其餘的函數可以執行正常,只是沒有做任何事情!

+0

謝謝你...我犯了愚蠢的錯誤,:)筆尖名稱是「bookdetailviewcontrollercell」,我正在使用我寫的上述內容。 – user720235 2011-05-18 12:52:42

+0

@ user720235:如果他的答案解決了您的問題,您應該將其標記爲已回答,以便他可以獲得信用並且其他人可以看到。 – 2011-05-18 13:12:48