iOS应用程序(此类不符合关键值编码dataSource)

9

我正在制作一款iPhone应用程序,我正在处理这个TableViewController,但在测试时,我遇到了这个错误,我不太知道该怎么办:

2012-01-13 13:45:32.947 HandHistory Reviews[3422:707] *** 
Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<SessionsTableViewController 0x191cb0> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key dataSource.'

你有什么想法吗?

这是我的SessionTableViewController.m文件:

#import "SessionsTableViewController.h"
#import "Session.h"

@interface SessionsTableViewController()

@property (nonatomic, copy) NSArray *sessions;

@end

@implementation SessionsTableViewController

@synthesize sessions = _sessions;

#pragma mark - View lifecycle

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

    NSMutableArray *sessions = [[NSMutableArray alloc] init];

    // Looking for files
    // Finding the Documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

    // Looping through each file in the directory
    for (NSString *file in directoryContent)
    {
        NSString *contents = [NSString stringWithContentsOfFile:[[paths objectAtIndex:0] stringByAppendingPathComponent:file] encoding:NSUTF8StringEncoding error:nil];

        Session *session = [[Session alloc] initWithName:file andContents:contents];

        [sessions addObject:session];
    }

    self.sessions = sessions;
}

#pragma mark - Table view data source

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //cell.textLabel.text = [[self.sessions objectAtIndex:indexPath.row] name];
    //cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%d hands", 10];

    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

先查看一下 NSLog 的会话。 - Hiren
3个回答

17

看起来你在界面构建器中的连线有误。你似乎将某些内容连接到了SessionsTableViewController的一个名为dataSource的插座上。我猜测你可能想要反过来做,因为我认为你在SessionsTableViewController中有一个表格视图。

所以,你需要将你的表格视图的dataSource属性连接到SessionsTableViewController的实例(在你的情况下可能是“文件拥有者”),而不是反过来。


我明白了,我将我的SessionsTableViewController设置为TableView的自定义类,而不是TableViewController,谢谢,这就是我想要的,现在一切都正常工作。 - Sander Declerck

1
通常情况下,当插座连接不正确时会出现此错误。在我的情况下,检查插座连接始终可以解决此问题。

0

请检查您在标有“模块”(位于类下方)的框中是否指定了错误的应用程序。这是在UI Builder中的Identity Inspector中。

通常情况下,您不应该指定任何模块,因为它默认为灰色条目“current....”,这意味着它在所有应用程序中都可用。但是,如果您在此处指定了应用程序,则UI项将对项目中的任何其他应用程序不可用,在尝试运行其他应用程序时会出现主题错误消息。

那就是我的问题...


如果这条评论回答了问题,它并不立即清楚如何解决。考虑重新措辞一下? - Tim Ogilvy
1
我不知道我还能用什么措辞来表达。如果你在模块中有任何东西,当你尝试构建另一个模块时,你将会得到OP指定的错误。 - music_and_cities

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