【问题标题】:Attempting to use table view in ios, getting error尝试在ios中使用表格视图,出现错误
【发布时间】:2012-03-24 02:27:59
【问题描述】:

我的 iOS 选项卡式应用程序中有一个表格视图,但我无法运行该应用程序,我在运行时收到此错误:

3/23/12 10:15:23.119 PM CodeCompanion: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "2-view-16" nib but didn't get a UITableView.'
 *** First throw call stack:
(0x13bd052 0x154ed0a 0x1365a78 0x13659e9 0x2436ae 0xda5cb 0xf5b89 0xf59bd 0xf3f8a 0xf3e2f 0xf1ffb 0xf285a 0xdbfbf 0xdc21b 0xdd0f1 0x4bfec 0x51572 0x4b72b 0x3abc2 0x3ace2 0x3aea8 0x41d9a 0x12be6 0x138a6 0x22743 0x231f8 0x16aa9 0x12a7fa9 0x13911c5 0x12f6022 0x12f490a 0x12f3db4 0x12f3ccb 0x132a7 0x14a9b 0x2888 0x27e5)

我的视图控制器头文件如下:

#import <UIKit/UIKit.h>

@class LanguageTableViewCell;

@interface LanguagesViewController : UITableViewController {

    NSArray *languages;

}

@property (nonatomic, retain) NSArray *languages;

@end

我的实现文件是:

#import "LanguagesViewController.h"
#import "LanguageTableViewCell.h"

@implementation LanguagesViewController

@synthesize languages;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"getting cell for row at index path: %i", [indexPath row]);
    LanguageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"languageBase"];
    if (cell == nil) {
        cell = [[LanguageTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault 
                reuseIdentifier:@"languageBase"];
    }
    [cell setName:[languages objectAtIndex:[indexPath row]]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"rows in section: %i is: %i", section, [languages count]);
    return [languages count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    NSLog(@"getting number of sections (1)");
    return 1;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"view loaded, setting array");
    languages = [NSArray arrayWithObjects:@"Java", nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

我对 xcode 和 Objective-c 还很陌生,所以请提及任何可能出错的地方。我已将表格视图的 ViewController 设置为我上面的类,并且在 xcode 中没有收到任何错误或警告。表格视图是使用原型单元格的动态视图之一。如果我需要发布单元格的类,我会的。

【问题讨论】:

  • 将超类从 UITableViewController 更改为 UIViewController。

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

从您的错误消息来看,您正在使用 nib 文件“2-view-16”来布局您的视图,但它不包含 UITableView(或未连接)。

LanguagesViewController 继承自 UITableViewController,它正在寻找一个 UITableView 来控制哪个不存在。

如果您使用 nib 来设置视图,则需要将 UITableView 添加到 nib 并将其设置为 LanguagesViewController 的 tableView。

【讨论】:

  • 我正在使用故事板。对不起我的无知,但那是不同的,对吧?
  • 啊,抱歉,我不熟悉 Storyboard,所以无法提供帮助。
  • @sampage: tableView 不是 UITableViewController 的暴露出口(够蠢),他必须自己创建,然后不仅设置为 tableView,还设置为视图。
  • 对我不起作用,我将自己的表格视图添加到屏幕上,然后尝试使用loadView 中的以下代码将其连接到 UITableViewController:self.tableView = self.foldersTableView; self.view = self.foldersTableView; [super loadView];。还覆盖- (UITableView *)tableView 以返回我自己的实例,但它甚至没有被调用。还是一样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多