iOS不符合关键值编码的uitableview

时间:2012-10-12 00:11:10

标签: ios uitableview key tableview

我有一个UIViewController,里面有一个UITableView。 UIViewController不是UITableViewController,因为视图上还有其他东西。如果我没有连接任何插座,则空表会与其他内容一起出现在视图中,没有错误。如果我将UIViewController的插座连接到UITableView,我会收到以下错误:

  

'[< UIViewController 0x748a420> setValue:forUndefinedKey:]:此类不是密钥tableStories的密钥值编码兼容。'

如果我还连接数据源并将UITableView的出口委托给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命令有关,但我一直在尝试堆栈中提到的其他解决方案来克服这个错误,到目前为止没有任何作用。 : - /

另外,我从另一个应用程序导入了故事板,所以也许这可能会造成一些麻烦。

2 个答案:

答案 0 :(得分:2)

我明白了。我在调试器输出的最开始出现错误

Unknown class IntroViewController in Interface Builder file.

我查看了StackExchange上的错误并找到了答案here它是Matthew给出了我使用的答案。我不得不将IntroViewController添加到编译源列表中。我不确定这意味着什么,或者为什么它没有阻止程序在没有表视图的情况下运行,但现在它可以工作了。谢谢StackExchange !!

答案 1 :(得分:1)

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

相关问题