将图像从TableView传递到UIView

时间:2014-05-21 02:36:01

标签: ios uitableview uiview uiviewcontroller parse-platform

我有一个tableView,我从Parse.com获取内容(图像,一个标题和一个描述),我使用UIView作为我的detailView。当点击一个单元格时,UIView会带来一个动画。我设法得到我的标题和事件描述,但我无法获得图像文件。

如何将解析后的图像传递给UIView?

感谢。

这是我的.h文件

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

@interface ViewController : UIViewController <UITableViewDelegate> {

    NSArray *events;
}
@property (weak, nonatomic) IBOutlet UITableView *newsTable;
@property (strong, nonatomic) IBOutlet UIView *detailView;
@property (strong, nonatomic) IBOutlet UILabel *InfoDetailLabel;
- (IBAction)backBtn:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *viewTitle;

这是.m文件

#import "ViewController.h"
#import "TableCell.h"
#import "Reachability.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize newsTable;


- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Pull to refresh

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.newsTable addSubview:refreshControl];


    //Checking connection
    if (![self connected])
    {
        // not connected
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Internet Connection Not Found" message:@"Please check your network settings!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    } else
    {
        // connected, do some internet stuff

    }

    // Do any additional setup after loading the view, typically from a nib.
    [self performSelector: @selector(retreiveFromParse)];

}

//pull to refresh
- (void)refresh:(UIRefreshControl *)refreshControl {
    [refreshControl endRefreshing];
}


- (void) retreiveFromParse {
    PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"News"];
    [retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            events = [[NSArray alloc] initWithArray:objects];
        }
        [newsTable reloadData];
    }];
}

//*********************Setup table of folder names ************************

//get number of sections in tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

//get number of rows by counting number of folders
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return events.count;
}

//setup cells in tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath             *)indexPath {

    //setup cell
    static NSString *CellIdentifier = @"EventCell";
    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    PFObject *tempObject = [events objectAtIndex:indexPath.row];

    cell.TitleLabel.text = [tempObject objectForKey:@"Event"];
    cell.DescriptionLabel.text = [tempObject objectForKey:@"Description"];

    // To get the image file from Parse class
    PFFile *imageFile = [tempObject objectForKey:@"imageFile"];
    PFImageView *imageView = [[PFImageView alloc] init];
    imageView.file = imageFile;

        [imageView loadInBackground:^(UIImage *img,NSError *error){

        if(!error)
        {
            UIImageView *yourImageView = [[UIImageView alloc] init];
            yourImageView.image = imageView.image;
            /*OR*/
            cell.imageView.image = imageView.image;
        }}];

    return cell;
}


//user selects folder to add tag to
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cell tapped");

    //For detail view
    PFObject *tempObject = [ events objectAtIndex:indexPath.row];
    NSLog(@"%@", tempObject.objectId);

    _InfoDetailLabel.text = [tempObject objectForKey:@"detailinformation"];
    [self animateDetailView];

    _viewTitle.text = [tempObject objectForKey:@"Event"];
    [self animateDetailView];


}
 //for animation of detailview

- (void) animateDetailView {
    [UIView animateWithDuration:0.3 animations:^{
        _detailView.frame = CGRectMake(0, 0, 320, 518);
    }];
}
//back button with animation
- (IBAction)backBtn:(id)sender {
    [UIView animateWithDuration:0.3 animations:^{
        _detailView.frame = CGRectMake(320, 0, 320, 518);
    }];
}



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


@end

和我的手机.h

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

@interface TableCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
@property (strong, nonatomic) IBOutlet PFImageView *imageView;


@end

1 个答案:

答案 0 :(得分:0)

尝试,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cell tapped");
    //Assuming not reordering the cells after it loaded
    TableCell *cell = (TableCell *)[tableView cellForRowAtIndexPath:indexPath];
    [[yourDetailView imageView] setImage:[[cell imageView] image]];
    .....

}

或者您可以使用loadInBackground:cellForRow中使用的相同方法detailView