Ios 将图像从CellForRowatineXpath获取到DidSelectRowatineXpath,以便在选中行时推送

Ios 将图像从CellForRowatineXpath获取到DidSelectRowatineXpath,以便在选中行时推送,ios,uitableview,parse-platform,Ios,Uitableview,Parse Platform,我正在尝试将图像从选定行推送到详细的viewController。这是我最近试过的。我还尝试从PFImageView获取PFFile,但没有成功 #import "ExploreViewController.h" #import "DetailExploreViewController.h" @interface ExploreViewController () @property (strong, nonatomic) PFGeoPoint *userLocation; @pro

我正在尝试将图像从选定行推送到详细的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

在您的
单元格中,您正在创建一个新的
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;
    }
然后,在您的
中选择rowatindexpath
-

- (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);

}

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

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toDetail"]) {
        DetailExploreViewController *destViewController = segue.destinationViewController;
        [destViewController setTheImage:self.theImage];
        [destViewController setTheString:self.theString];
    }
}
在细节控制器实现文件中,我将在
视图中设置细节视图图像,改为显示
方法,如下所示:

-(void)viewWillAppear:(BOOL)animated{

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

}

这对我来说很有用。

多亏@Paulw11的建议,我做到了这样:

ExploreViewController.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;

}
- (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
-(PFQuery*)queryForTable{
如果(!self.userLocation){
返回零;
}     
PFQuery*query=[PFQuery queryWithClassName:@“家庭人口”];
[query whereKey:@“geopoint”nearGeoPoint:self.userLocation with Inkilometers:15];
query.limit=25;
//在加载tableView的同时调用getData填充数组
[自我获取数据];
返回查询;
}
//从Parse中检索对象并将其添加到NSMutableArray中
-(void)getData{
PFQuery*query=[PFQuery queryWithClassName:@“家庭人口”];
[query whereKey:@“geopoint”nearGeoPoint:self.userLocation with Inkilometers:15];
[查询findObjectsInBackgroundWithBlock:^(NSArray*对象,NSError*错误){
如果(!错误){
用于(PFObject*对象中的对象){
Detailish*detail=[Detailish new];//Detailish是数组对象的一个类
detail.discovery=[objectobjectforkey:@“discovery”];
PFFile*getImage=[objectobjectforkey:@“imageFile”];
[getImage getDataInBackgroundWithBlock:^(NSData*数据,NSError*错误){
如果(!错误){
detail.image=[UIImage-imageWithData:data];
NSLog(@“细节想象:%@”,细节。想象);
}
}];
detail.location=[objectobjectforkey:@“location”];
[fordetailaddobject:detail];
}
}
}];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject)
*)反对{
静态NSString*simpleIdentifier=@“ExploreCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:simpleIdentifier];
}
cell.textLabel.text=[object objectForKey:@“发现”];
返回单元格;}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
if([segue.identifier IsequalString:@“toDetail”]){
DetailExploreViewController*destViewController=segue.destinationViewController;
NSIndexPath*indexPath=[self.tableView indexPathForSelectedRow];
NSLog(@“Halo ej:%@,[forDetail objectAtIndex:indexath.row]);
destViewController.hump=[forDetailObjectatindex:indexPath.row];
Detailish*details=[forDetailObjectatindex:indexath.row];
NSLog(@“discovery:%@”,details.discovery);
NSLog(@“image:%@”,details.imagine);
} }
DetailExploreViewController.h
#进口
#导入“Detailish.h”
@接口详细信息ExploreViewController:UIViewController
@性质(强的,非原子的)详细*驼峰;
@结束
DetailExploreViewController.m
#导入“DetailExploreViewController.h”
@接口详细信息ExploreViewController()
@属性(弱、非原子)IBUIImageView*图像视图;
@属性(弱,非原子)IBUILabel*标签;
@结束
@实现详细信息ExploreViewController
@综合标签、图像视图;
-(无效)视图将显示:(BOOL)动画
{
label.text=self.hump.discovery;
theImageView.image=self.hump.imagine;
}
详细说明
#进口
@接口详细信息:NSObject
@属性(强,非原子)NSString*发现;
@属性(强,非原子)NSString*位置;
@属性(强,非原子)UIImage*imagine;
@结束
Detailish.m
#导入“Detailish.h”
@实现细节
@综合发现、定位、想象;
@结束

感谢您的快速回复。不幸的是,这只是将图像设置为我点击的任何单元格上的单元格。我需要从此单元格中获取图像。imageView,以便将其推送到我的DetailViewController中显示。您是否询问如何将检索到的图像发送到您的DetailViewController?是的,这正是我需要的。您的详细视图是如何显示的?你在导航控制器里推它吗?使用故事板/序列图像?看起来还可以。您所缺少的只是[self-PerformsgueWithIdentifier:@“toDetail”];在didselectindexpath中。此外,在ViewWillDisplay中访问属性可能比在viewDidLoad中访问属性更好