1

我正在尝试从 Parse 后端获取加载有数据的表视图,以推送带有与表中数据关联的图像(PFFile)的详细视图。我可以很好地将数据加载到表中,但是当转到新的视图控制器时,应用程序崩溃了。这是我的 PFQueryTableViewController 实现中的代码。

- (id)initWithCoder:(NSCoder *)aDecoder
{

self = [super initWithClassName:@"Wine"];
self = [super initWithCoder:aDecoder];

if (self) {

    self.className = @"Wine";

    self.textKey = @"Name";

    self.pullToRefreshEnabled = YES;


    self.paginationEnabled = YES;

    self.objectsPerPage = 50;
    self.sections = [NSMutableDictionary dictionary];
    self.sectionToWineTypeMap = [NSMutableDictionary dictionary];
}
return self;
}

- (void)viewWillAppear:(BOOL)animated
{

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage   imageNamed:@"1.png"]];
[self.tableView setBackgroundView:imageView];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return self.sections.allKeys.count;
}


- (NSString *)wineTypeForSection:(NSInteger)section {
return [self.sectionToWineTypeMap objectForKey:[NSNumber numberWithInt:section]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
NSString *wineType = [self wineTypeForSection:section];
NSArray *rowIndecesInSection = [self.sections objectForKey:wineType]; return   rowIndecesInSection.count;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {
return 40;
}  


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor groupTableViewBackgroundColor];
sectionHeader.textAlignment = UITextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:17];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = [self wineTypeForSection:section];
return sectionHeader;
}

- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];


[self.sections removeAllObjects];
[self.sectionToWineTypeMap removeAllObjects];

NSInteger section = 0;
NSInteger rowIndex = 0;
for (PFObject *object in self.objects) {
    NSString *wineType = [object objectForKey:@"wineType"];
    NSMutableArray *objectsInSection = [self.sections objectForKey:wineType];
    if (!objectsInSection) {
        objectsInSection = [NSMutableArray array];

        [self.sectionToWineTypeMap setObject:wineType forKey:[NSNumber   numberWithInt:section++]];
    }

    [objectsInSection addObject:[NSNumber numberWithInt:rowIndex++]];
    [self.sections setObject:objectsInSection forKey:wineType];
}
}


- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.className];

if (self.pullToRefreshEnabled) {
    query.cachePolicy = kPFCachePolicyNetworkOnly;
}

if (self.objects.count == 0) {
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}

[query orderByAscending:@"orderIndex"];

return query;
}

- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *wineType = [self wineTypeForSection:indexPath.section];

NSArray *rowIndecesInSection = [self.sections objectForKey:wineType];

NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
return [self.objects objectAtIndex:[rowIndex intValue]];
}

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

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

cell.textLabel.text = [object objectForKey:@"Name"];
cell.detailTextLabel.text = [object objectForKey:@"Price"];

return cell;
}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


if ([[segue identifier] isEqualToString:@"ShowWine"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    PFFile *file = [object objectForKey:@"image"];
    [[segue destinationViewController] setFile:file];

}
}

这是来自详细视图控制器标题和实现的代码

@property (nonatomic, retain) PFFile *file;
@property (nonatomic, retain) PFImageView *detailImage;

执行

@implementation DetailVC
@synthesize file;
@synthesize detailImage;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

-(void)viewDidAppear:(BOOL)animated

{
detailImage.file = file;

// Now tell PFImageView to download the file asynchronously
[detailImage loadInBackground];
}

- (void)viewDidUnload
{

[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

这是我收到的错误消息。

由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合键详细信息的键值编码。”

4

1 回答 1

1

可能仍然存在与detail故事板内部命名的现在不存在的 IBOutlet 的连接。

打开你的故事板,打开连接检查器(按 CMD+Option+6)并检查你的 viewControllers 的连接是否仍然链接到detail.

只需删除该连接。

于 2012-09-23T18:29:52.750 回答