【问题标题】:UIImageView not displaying set image from UITableViewControllerUIImageView 不显示来自 UITableViewController 的设置图像
【发布时间】:2012-03-08 23:37:57
【问题描述】:

我有一个带有图像名称列表的UITableViewController。单击名称时,图像的 URL 设置在 ImageViewController 中,然后 UITableViewController 与包含 UIImageViewImageViewController (与插座相连)。在ImageViewControllerviewDidLoad 中,我将UIImageView 设置为来自URL 的图像,但是当我在模拟器上运行它时,只有一个黑屏出现在图像应该出现的位置。

对转场的一点视觉描述(其中 -> 表示转场):

  • ImageListTableViewController –> ImageViewController(有UIImageView)

ImageListTableViewController.m

// The delegate here is the ImageViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Show Image"]) {
        [self setDelegate:segue.destinationViewController];
    }
}
// getting URL and calling the protocol method (in ImageViewController) which sets
// the image as an @property
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *imageDict = [self.imageArray objectAtIndex:indexPath.row];
    NSURL *imageURL = [FlickrFetcher urlForPhoto:imageDict format:FlickrPhotoFormatLarge];
    NSLog([imageURL description]);
    [self.delegate imageListTableViewController:self withURL:imageURL];
}

ImageViewController.m

#import "ImageViewController.h"

@implementation ImageViewController

@synthesize image = _image;
@synthesize imageView = _imageView;

- (void)imageListTableViewController:(ImageListTableViewController *)sender 
                             withURL:(NSURL *)imageURL;
{
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    self.image = [UIImage imageWithData:imageData];
}

#pragma mark - View lifecycle


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
  // nothing
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.imageView setImage:self.image];
}


- (void)viewDidUnload
{
    [self setImageView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end

对正在发生的事情有任何想法吗? 谢谢!

【问题讨论】:

    标签: ios uiimageview imageview uitableview


    【解决方案1】:

    使用委托并不是真正正确的方式。试试这个: 在ImageViewController.h:

    @property(nonatomic,strong) NSURL *imageURL;
    

    ImageViewController.m

    @synthesize imageURL;
    

    移除 imageListTableViewController

    更改viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        self.image = [UIImage imageWithData:imageData];
    }
    

    添加到viewDidUnload:

    [self setImageURL:nil];
    

    然后在您的ImageListTableViewController.m 中: 换prepareForSegue

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"Show Image"]) {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSDictionary *imageDict = [self.imageArray objectAtIndex:indexPath.row];
            NSURL *imageURL = [FlickrFetcher urlForPhoto:imageDict format:FlickrPhotoFormatLarge];
    
            // Set the property on our Dest VC
            ImageViewController *ivc = segue.destinationViewController;
            ivc.imageURL = imageURL;
    
        }
    }
    

    更改您的didSelectRowAtIndexPath

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self performSegueWithIdentifier:@"Show Image" sender:nil];
    }
    

    确保您已将该 Segue 连接到 VC 本身,而不是连接到 TableView。如果您不这样做,Segue 将在 didSelectRowAtIndexPath 被解雇之前被解雇。在这种情况下,它不会产生影响,但对于真正的控制 - 养成连接到 VC 并在需要时调用 performSegue 的习惯。

    【讨论】:

    • 非常感谢 ElJay,我回家后试试这个。为了将来参考,什么时候是使用代表的合适时间?我正在关注斯坦福的系列讲座,据我所知,它们是以这种方式使用的。
    • 委托是告诉班级你已经完成某事的好方法。它们可用于让调用类知道某事已完成并可以将对象传回(例如当您登录用户并需要传回 UserInfo 时)。它们并没有真正被用来(根据我的经验)来调用某些东西。顺便说一句,斯坦福的视频很棒。
    • 谢谢。好的,尝试此操作后,我仍然只是黑屏。此外,继续图像需要更长的时间。想法?
    • 哈哈,哇,好尴尬。 dl.dropbox.com/u/13903949/Flickr%20Tops.zip
    • 再次感谢 ElJay。我在您的网站上找不到任何联系信息,所以我只是在某个地方发布(认为它可能会向您发送电子邮件)。
    【解决方案2】:

    尝试调用这个方法:

    [self.imageView setImage:self.image];
    

    在-

    (void)imageListTableViewController:(ImageListTableViewController *)sender 
                                 withURL:(NSURL *)imageURL;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多