2011-05-01 101 views
0

我正在開發基於導航的應用程序。 我RootViewController的包含3個小區 - 當第一個被按下一個UIViewController被推(這適用) -The的問題是與將要用於推動一個UITableViewControlleriphone UITableVIew(基於導航)

該應用沒有運行第二和第三細胞錯誤,它不會崩潰,但是當我導航到tableview時,查看一個沒有任何元素的空表。

這部分代碼有問題嗎? :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"Introduction" bundle:nil]; 
    [email protected]"Introduction"; 

    UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:@"Pictures" bundle:nil]; 
    [email protected]"Pictures"; 

    UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:@"ImagesViewController" bundle:nil]; 
    [email protected]"Images"; 

    // Pass the selected object to the new view controller. 
    if (0 == indexPath.row) 
    [self.navigationController pushViewController:detailViewController animated:YES]; 

    if (1== indexPath.row) 
    [self.navigationController pushViewController:myPictures animated:YES]; 

    if (2== indexPath.row) 
    [self.navigationController pushViewController:myImages animated:YES]; 

    [detailViewController release]; 
    [myPictures release]; 
    [myImages release]; 
+1

您應該創建viewControllers僅當實例相應的行已被選擇來最大限度地減少你的內存佔用量,你能解釋一下你的意思嗎?「但是當我導航到tableview」時,你的意思是,通過點擊應該顯示viewController的單元格嗎? – 2011-05-01 13:00:15

+0

yes,c舔細胞.. – 2011-05-01 13:45:39

回答

0

你在做什麼這是非常錯誤的(除了你原來的問題)。你爲什麼要實例化每個視圖控制器,然後只使用基於當前「單元格選擇」的視圖控制器?這會降低您的應用程序的速度,具體取決於加載這些單獨視圖所需的時間。你應該只在裏面實例化相關的視圖控制器「if(indexPath.row == 2){」塊。

除此之外,您的方法還有很多錯誤。自從實例化一個通用的UITableViewController(即使你提供了自己的筆尖)你顯然只會向你顯示一個空的視圖。你需要有一個你自己的類,這個筆尖被綁定爲一個委託,然後它將爲TableView提供數據。

我相信當你創建這些nib文件(例如「圖片」)xcode也給你一個'PicturesViewController.h和PicturesViewController.m'文件?如果是這樣,你需要在那裏寫適當的代碼,並確保Tableview 。在「圖片」筆尖文件都有「數據源」,並設置爲「PicturesViewController」代表'然後,當你想證明觀點做到這一點,而不是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.row == 1) { 
     ... 
    } else if (indexPath.row == 2) { 
    PicturesViewController *myPictures = [[PicturesViewController alloc] initWithNibName:@"Pictures" bundle:nil]; 

    [self.navigationController pushViewController:myPictures animated:YES]; 
    [myPictures release]; 
    } 

} 
+0

感謝您的回覆, 好吧,我糾正了我的代碼+在我的picturesviewcontroller.h&.mi使用的項目數組來顯示和「圖片」筆尖文件中的tableview有其'數據源'和'代表'設置爲'picturesviewcontroller'。 但你的代碼是給了一個錯誤「意外接口pictureViewController」 我怎麼能做到這一點:「你需要有一個類你自己的哪個筆尖綁作爲一個代表,然後將提供的TableView與數據。「 – 2011-05-01 13:28:52

+1

不看你的代碼我不能說。然而,這只是意味着你已經命名你的類不同,你使用我的代碼逐字不會工作。這只是作爲一個例子。請將我的代碼中的所有類名稱替換爲您使用的實際名稱。還有一個'接受'的答案將不勝感激。 – strange 2011-05-02 13:07:27

+0

lool ..我仍然是新的iphone開發thingy但我不是愚蠢的使用相同名稱的類作爲例子使用:P – 2011-05-03 13:09:24