【问题标题】:Xcode not compiling "Terminating app due to uncaught exception 'NSInternalInconsistencyException'"Xcode 未编译“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序”
【发布时间】:2012-03-19 06:34:12
【问题描述】:

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

我的代码如下所示:

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 在编译之前没有标记任何错误,但是当我尝试这样做时,它会在控制台中显示这一点并且它不会编译我的应用程序。

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

谁能告诉我这是怎么回事?

【问题讨论】:

    标签: ios xcode ios5 uitableview


    【解决方案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;
        }
    

    【讨论】:

    • 感谢成功,抱歉我是 iOS 开发的新手。
    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 2012-07-28
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多