ios - Xcode 不编译 "Terminating app due to uncaught exception ' NSInternalInconsistencyException'”

标签 ios xcode ios5 uitableview

我在尝试在我的 iOS 应用程序中创建一个简单的 TableView 时遇到了很多问题。我从更复杂的源代码开始,但在跟踪错误时我发现它是 TableView Controller 的数据源方法中的错误。所以我用一个简单的 Nav Controller 和一个 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 个表示“你好”的单元格的列表。

    - (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:'

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

最佳答案

将此方法更改为

 - (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 - Xcode 不编译 "Terminating app due to uncaught exception ' NSInternalInconsistencyException'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765950/

相关文章:

objective-c - 单例被释放

xcode - 你如何让网络进度微调器显示在 iOS 中?

ios - While Loop in Swift 需要声明?

c# - 在允许更多触摸识别之前等待 TouchesEnded 中的操作完成 - Xamarin iOS

ios - 在 Objective C 中动态修改 UICollectionViewCell 中单元格的高度

ios - 如何使用 Inspector 在 Xcode 中缩放 3D 对象?

iphone - Xcode 4.2 和 iOS 5 上为 "no provisioned ios devices are available"

ios - 将 HTML 转换为属性字符串时 NSAttributedString 崩溃

xcode - UIImagePickerController 的相机覆盖 View 未正确显示?

ios - 不同的颜色,即使 RGB 值相等