我在尝试在我的 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/