繁体   English   中英

Xcode无法编译“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序”

[英]Xcode not compiling “Terminating app due to uncaught exception 'NSInternalInconsistencyException'”

尝试在iOS应用程序中创建简单的表格视图时遇到了很大的问题。 我从更复杂的源代码开始,但是在追踪错误的同时,我发现这是Table View Controller的数据源方法中的错误。 因此,我用一个简单的Nav控制器和一个Table View制作了一个非常简单的应用程序,我什至没有设法使其运行。

这是我的代码:

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个表示“ Hello”的单元格的列表。

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

        cell.textLabel.text = @"Hello";

        return cell;
    }

Xcode在编译之前没有标记任何错误,但是当我尝试执行此操作时,它会在控制台中显示此错误,并且不会编译我的App。

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

谁能告诉我这是怎么了?

将此方法更改为

 - (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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM