简体   繁体   English

从Tableview单元格到详细图像视图的Parse.com Segue

[英]Parse.com Segue from Tableview Cell to Detail Image View

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. 我试图获取一个表视图,该表视图从Parse后端加载了数据,以推送带有与表中数据关联的图像(PFFile)的详细视图。 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. 这是我的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];

}
}

And here is the code from the Detail view controller header and implementation 这是Detail视图控制器头和实现中的代码

@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.' 由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类与密钥详细信息的编码不兼容。

It is possible that there is still a connection to a, now non existing, IBOutlet named detail inside of your storyboard. 情节图板内部可能仍然与现在不存在的IBOutlet命名detail存在连接。

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 . 打开情节提要,打开“连接检查器”(按CMD + Option + 6),然后检查viewControllers的连接是否存在仍链接到detail的无效连接。

Just remove that connection. 只需删除该连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM