将图像从 TableView 传递到 UIView

Posted

技术标签:

【中文标题】将图像从 TableView 传递到 UIView【英文标题】:Passing image from TableView to UIView 【发布时间】:2014-05-21 02:36:00 【问题描述】:

我有一个 tableView,我从 Parse.com 获取内容(一张图片、一个标题和一个描述),我正在为我的 detailView 使用 UIView。当点击一个单元格时,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】:

试试,

- (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]];
    .....


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

【讨论】:

@user3499983 你是否在cellForRowAtIndexPath: 中设置cell.imageView.image = img;

以上是关于将图像从 TableView 传递到 UIView的主要内容,如果未能解决你的问题,请参考以下文章

iPhone SDK如何将tableview中plist中的选定行传递到UIViewController

如何将图像从 TableViewCell 传递到 UIImage

手势识别器和 TableView

将数据从 tableview 传递到另一个 tableview

无法将 UIView 定位到 TableView 的底部

将数据从 Tableview 传递到 Tableview 详细信息