iOS不支持键值编码兼容的UITableView。

3
我有一个UIViewController,其中包含一个UITableView。由于视图上还有其他东西,所以该UIViewController不是UITableViewController。如果我不连接任何outlet,则空表格将显示在视图上,同时还有其他内容,没有错误。如果我将UIViewController的outlet与UITableView连接起来,则会出现以下错误:

'[<UIViewController 0x748a420> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableStories.'

如果我还将UITableView的datasource和delegate outlet也连接到这个UIViewController中,则会得到相同的错误。这是该视图控制器的.h文件:
#import <UIKit/UIKit.h>

@interface IntroViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, weak) IBOutlet UITableView *tableStories;
@property (nonatomic, strong) NSManagedObjectContext* managedObjectContext;
@property (nonatomic, strong) NSArray *stories;

这里是 .m 文件。

@implementation IntroViewController

@synthesize tableStories;
@synthesize stories;
@synthesize managedObjectContext;


- (void)viewDidLoad
{
    [super viewDidLoad];

    tableStories.delegate = self;
    tableStories.dataSource = self;

    managedObjectContext = ((ReadMeAppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"RMStory" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error;
    self.stories = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [stories count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    RMStory *thisStory = [stories objectAtIndex:indexPath.row];
    cell.textLabel.text = thisStory.title;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", thisStory.author];

    return cell;
}

可能与我的Core Data命令有关,但我已经尝试了堆栈中提到的其他解决方案来解决这个错误,但目前为止还没有任何作用。 :-/

另外,我从另一个应用程序导入了故事板,所以可能会引起一些麻烦。


只是为了明确,我没有连接任何其他插座或任何其他设备到所提到的插座上。 - syzygy
2个回答

2
我明白了。我在调试器输出的一开头收到一个错误信息。
Unknown class IntroViewController in Interface Builder file.

我在 StackExchange 上查找了错误并找到了答案,这里提供了答案。回答的人是Matthew。我必须将 IntroViewController 添加到编译源列表中。我不确定这意味着什么,或者为什么在缺少表视图的情况下它没有阻止程序运行,但现在它可以工作了。感谢 StackExchange!

如果您没有声明 @implementation,也可能会发生这种情况。我在 https://dev59.com/z3I-5IYBdhLWcg3wsKl4#12755891 和 https://dev59.com/D2cs5IYBdhLWcg3wgkQQ#22797318 上进一步讨论了这个问题。 - Zack Morris

1
在IB中,不要忘记将你的UIViewController类设置为IntroViewController

我进行了双重检查,将其更改并重新连接了所有插座,但仍然出现相同的错误。不过还是谢谢! - syzygy
如果是这样,那你是否检查过你的视图控制器所使用的 init 方法是否真正加载了相应的 Nib 文件? - John Estropia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接