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

San*_*rck 8 key-value uitableview ios

我正在制作一个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.'
Run Code Online (Sandbox Code Playgroud)

有人有个主意吗?

这是我的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
Run Code Online (Sandbox Code Playgroud)

mat*_*way 16

看起来你已经错误地在界面构建器中连接了一些东西.看来你已经把东西贴dataSource在了你的电话中SessionsTableViewController.看起来你可能想要反过来做,因为我假设你有一个表视图SessionsTableViewController.

因此,您需要将dataSource表视图的属性附加到您的实例SessionsTableViewController(在您的情况下可能是"文件所有者"),而不是相反.