I am trying to get a table view loaded with data from a Parse backend to push a detail view with an image (PFFile) associated with the data in the table. I can get the data to load into the table fine but when segueing to new view controller the app crashes. Here is code from my PFQueryTableViewController implementation.
- (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];
}
}
And here is the code from the Detail view controller header and implementation
@property (nonatomic, retain) PFFile *file;
@property (nonatomic, retain) PFImageView *detailImage;
Implementation
@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
And here is the error message I am getting.
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key detail.'
It is possible that there is still a connection to a, now non existing, IBOutlet named detail
inside of your storyboard.
Open your storyboard, open the Connection Inspector (press CMD+Option+6) and check the connections of your viewControllers for an invalid connection that is still linked to detail
.
Just remove that connection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.