2011-12-13 107 views
2

我使用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沒有錯誤,因爲它的工作原理是如果我試圖從按鈕推它。請問我需要關於這個問題的建議,我現在就這個問題進行了討論。謝謝大家。

回答

6

注知道如何有利,這是..但就在今天我遇到了麻煩,從一個tableview中移動到另一個視圖單擊行以後。

我住從didSelectRowAtIndexPath方法完全程。相反,我使用了賽格(這正是我所瞄準的)。

這段代碼是從使用表的蘋果故事板示例中截取的。

http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Listings/Classes_RootViewController_m.html#//apple_ref/doc/uid/DTS40007416-Classes_RootViewController_m-DontLinkElementID_10

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"ShowSelectedPlay"]) { 
      NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow]; 
      DetailViewController *detailViewController = [segue destinationViewController]; 
      detailViewController.play = [dataController objectInListAtIndex:selectedRowIndex.row]; 
    } 
} 

這一切都assumign你開始在故事板功能使用塞格斯,並確保使用的標識符的塞格斯。

+0

我認爲這是正確的 - 在上面的代碼中的問題是,新的表視圖控制器正在分配/直接插入,並沒有從故事板加載,這意味着原型單元格不可用。在prepareForSegue中使用segues並配置目標視圖控制器將解決這個問題,或者它可以從storyboard中實例化,但這似乎是不必要的代碼。 – jrturton 2011-12-13 20:58:13

0

您只需要在設計器中首先創建一個原型單元格,以便您可以將單元格從單元格拖出到目標視圖。