我使用xcode 4.2和storyboard創建了一個iOS選項卡式應用程序。我加了一個tableviewcontroller定製的電池就可以了,點擊該行的時候,我想開一個tableviewcontroller,我用下面xcode 4.2 iOS桌面使用故事板
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
categoryClass *cc = [datas objectAtIndex:indexPath.row];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
iSubProducts *subProducts = [[iSubProducts alloc] init];
subProducts.title = cc.categoryName;
subProducts.catID = cc.categoryID;
[[self navigationController] pushViewController:subProducts animated:YES];
[subProducts release];
}
下面的代碼,但是當我點擊了排它給了我下面的錯誤:
*終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',理由是:「數據源的UITableView必須返回從的tableView細胞:在我iSubProducts tableviewcontroller的cellForRowAtIndexPath
,我有如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCell2";
iSubProductsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
productSubClass *cc = [datas2 objectAtIndex:indexPath.row];
NSLog(@"Product Name: %@", cc.productName);
cell.txtProductName.text = cc.productName;
cell.txtProductDesc.text = cc.productDesc;
return cell;
}
我假設這是錯誤發生的位置,單元格返回一個零值。當我嘗試使用或從一個按鈕附加iSubProducts tableviewcontroller時,它一切正常,但如果它來自行點擊,出現此錯誤。
在iOS開發中,我很新,也許有一個錯誤從tableviewcontroller打開tableviewcontroller並帶有自定義單元格。我一直在抱怨我的頭2天,並搜索了很多,不幸的是我沒有找到任何解決方案。我敢肯定,iSubProducts的tableviewcontroller沒有錯誤,因爲它的工作原理是如果我試圖從按鈕推它。請問我需要關於這個問題的建議,我現在就這個問題進行了討論。謝謝大家。
我認爲這是正確的 - 在上面的代碼中的問題是,新的表視圖控制器正在分配/直接插入,並沒有從故事板加載,這意味着原型單元格不可用。在prepareForSegue中使用segues並配置目標視圖控制器將解決這個問題,或者它可以從storyboard中實例化,但這似乎是不必要的代碼。 – jrturton 2011-12-13 20:58:13