从cellForRowAtIndexPath获取图像到didSelectRowAtIndexPath以在选择行时推送

时间:2014-05-03 02:47:19

标签: ios uitableview parse-platform

我正在尝试将图像从选定的行推送到详细的viewController。这是我最近尝试过的。我也尝试从PFImageView获取PFFile但不成功。

 #import "ExploreViewController.h"
 #import "DetailExploreViewController.h"

 @interface ExploreViewController ()

 @property (strong, nonatomic) PFGeoPoint *userLocation;
 @property (weak, nonatomic) UIImage *imagine; 
 @property (weak, nonatomic) NSString *discovery;

 @end


    - (PFQuery *) queryForTable
{
    if (!self.userLocation) {
        return nil;
    }

    PFQuery *query = [PFQuery queryWithClassName:@"HomePopulation"];
    [query whereKey:@"geopoint" nearGeoPoint:self.userLocation withinKilometers:15];
    query.limit = 25;

    //[self getLocation];

    return query;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
    }

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


    PFFile *getImage = [object objectForKey:@"imageFile"];
    [getImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            cell.imageView.image = [UIImage imageWithData:data];

            NSLog(@"hojla: %@", cell.imageView.image);
        } else {
            cell.imageView.image = [UIImage imageNamed:@"1bar.jpg"];
        }
    }];

    return cell;
}

- (void)tableView:(UITableView *)tableViewer didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableViewer cellForRowAtIndexPath:indexPath];

    self.discovery = cell.textLabel.text;
    NSLog(@"the text: %@", self.discovery);

    self.imagine = cell.imageView.image;

    NSLog(@"ajde: %@", self.imagine);

}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toDetail"]) {
        DetailExploreViewController *destViewController = segue.destinationViewController;
        destViewController.theImage = self.imagine;
        destViewController.theString = self.discovery;
    }
}

DetailExploreViewController.h

#import <UIKit/UIKit.h>

@interface DetailExploreViewController : UIViewController
@property (strong, nonatomic) NSString *theString;
@property (strong, nonatomic) UIImage *theImage;

@end

DetailExploreViewController.m

#import "DetailExploreViewController.h"

@interface DetailExploreViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *theImageView;
@property (weak, nonatomic) IBOutlet UILabel *theLabel;

@end

@implementation DetailExploreViewController

@synthesize theString, theLabel, theImageView, theImage;


- (void)viewDidLoad
{
    [super viewDidLoad];

    theLabel.text = theString;
    theImageView.image = theImage;

}

3 个答案:

答案 0 :(得分:0)

在您的cellForRowAtIndexPath中,您正在创建一个新的UIImageView来包含您的图片,但不会对其进行任何操作。获得图像后,您只需将其分配给单元格的imageView.image属性 -

即可
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
    {
        static NSString *simpleIdentifier = @"ExploreCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
        }

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

        PFFile *getImage = [object objectForKey:@"imageFile"];
        [getImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!error) {
                cell.imageView.image = [UIImage imageWithData:data];
                NSLog(@"hojla: %@", cell.imageView.image);
            }
            else {
               //TODO - Put some placeholder/"not found" image into cell.imageView.image
            }
        }];

        return cell;
    }

然后,在didSelectRowAtIndexPath -

- (void)tableView:(UITableView *)tableViewer didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableViewer cellForRowAtIndexPath:indexPath];

    UILabel *label = cell.textLabel
    NSLog(@"the text: %@", label.text);

    UIImageView *img = cell.imageView;

    NSLog(@"ajde: %@", img.image);

}

答案 1 :(得分:0)

尝试向详细控制器发送消息,而不是使用点语法,如下所示:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toDetail"]) {
        DetailExploreViewController *destViewController = segue.destinationViewController;
        [destViewController setTheImage:self.theImage];
        [destViewController setTheString:self.theString];
    }
}

在详细控制器实现文件中,我会在viewWillAppear方法中设置详细视图图像,如下所示:

-(void)viewWillAppear:(BOOL)animated{

    theLabel.text = theString;
    theImageView.image = theImage;

}

这适合我。

答案 2 :(得分:0)

感谢@ Paulw11的建议,我做到了这样:

ExploreViewController.m

- (PFQuery *) queryForTable {
    if (!self.userLocation) {
        return nil;
    }     
    PFQuery *query = [PFQuery queryWithClassName:@"HomePopulation"];
    [query whereKey:@"geopoint" nearGeoPoint:self.userLocation withinKilometers:15];

    query.limit = 25;

    // Calling getData to populate the array at the same time as the tableView is being loaded
    [self getData];
    return query;

}

//Retrieving objects from Parse and adding them in a NSMutableArray
- (void)getData { 
    PFQuery *query = [PFQuery queryWithClassName:@"HomePopulation"];
    [query whereKey:@"geopoint" nearGeoPoint:self.userLocation withinKilometers:15];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {
                Detailish *detail = [Detailish new]; // Detailish is a class for the array objects
                detail.discovery = [object objectForKey:@"discovery"];
                PFFile *getImage = [object objectForKey:@"imageFile"];
                [getImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                    if (!error) {
                        detail.imagine = [UIImage imageWithData:data];
                        NSLog(@"Detail imagine: %@", detail.imagine);
                    }
                }];
                detail.location = [object objectForKey:@"location"];

                [forDetail addObject:detail];
            }
        }
    }];

}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
    }

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


    return cell; }


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"toDetail"]) {
        DetailExploreViewController *destViewController = segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSLog(@"Halo ej: %@", [forDetail objectAtIndex:indexPath.row]);
        destViewController.hump = [forDetail objectAtIndex:indexPath.row]; 

        Detailish *details = [forDetail objectAtIndex:indexPath.row];
        NSLog(@"discovery: %@", details.discovery);
        NSLog(@"image: %@", details.imagine);
    } }

DetailExploreViewController.h
#import <UIKit/UIKit.h>
#import "Detailish.h"

@interface DetailExploreViewController : UIViewController

@property (strong, nonatomic) Detailish *hump;

@end

DetailExploreViewController.m
#import "DetailExploreViewController.h"

@interface DetailExploreViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *theImageView;
@property (weak, nonatomic) IBOutlet UILabel *theLabel;

@end

@implementation DetailExploreViewController

@synthesize theLabel, theImageView;


- (void) viewWillAppear:(BOOL)animated
{
    theLabel.text = self.hump.discovery;
    theImageView.image = self.hump.imagine;
}

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

@interface Detailish : NSObject

@property (strong, nonatomic) NSString *discovery;
@property (strong, nonatomic) NSString *location;
@property (strong, nonatomic) UIImage *imagine;

@end

Detailish.m
#import "Detailish.h"

@implementation Detailish

@synthesize discovery, location, imagine;

@end