2012-03-19 70 views
1

我有一個相當一個問題,試圖在我的iOS應用程序中做一個簡單的表視圖。我從一些更復雜的源代碼開始,但在追蹤錯誤時,我發現它是Table View Controller的數據源方法中的錯誤。所以我用一個簡單的導航控制器和一個表格視圖做了一個非常簡單的應用程序,我甚至還沒有設法讓它運行。Xcode不編譯「終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException'」

這裏是我的代碼如下所示:

AppDelegate.m

#import "BIDAppDelegate.h" 

    #import "BIDFirstLevelController.h" 

    @implementation BIDAppDelegate 

    @synthesize window = _window; 
    @synthesize navController = _navController; 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     // Override point for customization after application launch. 
     BIDFirstLevelController *firstView = [[BIDFirstLevelController alloc] initWithStyle:UITableViewStylePlain]; 
     self.navController = [[UINavigationController alloc] initWithRootViewController:firstView]; 
     self.window.rootViewController = self.navController; 
     [self.window makeKeyAndVisible]; 
     return YES; 
    } 

下面是引發問題的方法。爲了測試它,我只想要一個顯示3個單元格的列表,表示「你好」。在編譯之前

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     cell.textLabel.text = @"Hello"; 

     return cell; 
    } 

Xcode的標記沒有錯誤,但是當我嘗試這樣做,就說明這個控制檯,它不會編譯我的應用程序。

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 

有誰能告訴我這有什麼問題嗎?

+0

這是一個運行時錯誤.. – 2012-03-19 06:47:21

回答

1

更改此方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44) reuseIdentifier:CellIdentifier]; 
     } 
     cell.textLabel.text = @"Hello"; 
     return cell; 
    } 
+0

感謝那些工作,對不起,我在iOS開發一種新的。 – 2012-03-19 18:10:18

+0

那好吧... – 2012-03-20 03:07:43

相關問題