将parse.com云后端数据从TableView传递给DetailViewController

时间:2013-12-28 23:04:28

标签: ios objective-c uitableview parse-platform detailview

我的解析后端应用程序出了问题。

On Parse我有一个这样的类:name(string),imageFile(File)和help(string)。

除了图像之外,一切都很好。当我点击一个单元格时,我被推到DetailViewController,出现2个字符串,但ImageView(PFImageView)为空。 ImageView不显示我的图像。 也许传递图像文件有问题?

这是我的代码:

MechanikViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface MechanikViewController : PFQueryTableViewController

@end

MechanikViewController.m:

#import "MechanikViewController.h"
#import "MechanikDetailViewController.h"
#import "Mechanik.h"

@interface MechanikViewController ()

@end

@implementation MechanikViewController

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

-(id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) {
        self.parseClassName = @"Mechanik";

        self.textKey = @"name";

        self.pullToRefreshEnabled = YES;

        self.paginationEnabled = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

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

    if ([self.objects count] == 0) {
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }
    [query orderByAscending:@"name"];

    return query;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [object objectForKey:@"name"];

    return cell;
}

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

    NSLog(@"error: %@", [error localizedDescription]);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        MechanikDetailViewController *destViewController = segue.destinationViewController;

        PFObject *object = [self.objects objectAtIndex:indexPath.row];
        Mechanik *mechanik = [[Mechanik alloc] init];
        mechanik.name = [object objectForKey:@"name"];
        mechanik.imageFile = [object objectForKey:@"imageFile"];
        mechanik.help = [object objectForKey:@"help"];


        destViewController.mechanik = mechanik;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

MechanikDetailViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "Mechanik.h"


@interface MechanikDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet PFImageView *formelImage;
@property (weak, nonatomic) IBOutlet UILabel *helpLabel;

@property (nonatomic, strong) Mechanik *mechanik;

@end

MechanikDetailViewController.m:

#import "MechanikDetailViewController.h"

@interface MechanikDetailViewController ()

@end

@implementation MechanikDetailViewController

@synthesize formelImage;
@synthesize helpLabel;
@synthesize mechanik;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = mechanik.name;
    self.helpLabel.text = mechanik.help;
    self.formelImage.file = mechanik.imageFile;


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Mechanik.h:

#import <Foundation/Foundation.h>
#import <Parse/Parse.h>

@interface Mechanik : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) PFFile *imageFile;
@property (nonatomic ,strong) NSString *help;

@end

Mechanik.m:

#import "Mechanik.h"

@implementation Mechanik

@synthesize name;
@synthesize imageFile;
@synthesize help;

@end

请帮帮我。

1 个答案:

答案 0 :(得分:0)

  • 在DetailViewController中创建一个PFObject

    @property (nonatomic, strong)PFObject *object
    
  • 将PFObject从master传递给详细信息

    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    destViewController.object = object;
    
  • 在ViewDidAppear中设置数据

    pfImageView.imageFile = [object objectForKey:@"imageFile"];
    [pfImageView loadInBackground];