iOS如何在模型更改时刷新ScrollView?

ifo*_*ght 6 iphone objective-c uiscrollview ios

我是iOS编程的新手,对iOS和Objective-C的知识有限,所以请给我一些帮助.

我正在开发一个flickr照片应用程序,它基本上需要来自flickr API的照片数据并在scrollView中显示一个.

但我在我的iPad版本遇到了一些问题,与UISplitViewController一起呈现.让我简单介绍一下我的应用程序.

MASTER VIEW:嵌入在UINavigationViewController中的UITableViewController列表,基本上它只是使用一些Flickr API来获取一些数据,如下所示:

navigationController - >

tableViewControllerA(按国家组织的照片) - >

tableViewControllerB(该国家/地区的照片列表)当在TableViewControllerB中选择照片时,在我的代码中,我将照片URL(来自flickr API)传递给我的详细视图控制器,这是一个scrollView,这是我在scrollView的ViewController中的代码.

IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *imageView;
NSURL *photoURL;


- (void) viewDidLoad
{
    [super viewDidLoad];

    dispatch_queue_t downloadQueue = dispatch_queue_create("flickr donwloader", NULL);
dispatch_async(downloadQueue, ^{
    // load image
    self.imageView.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:self.photoURL]];

    dispatch_async(dispatch_get_main_queue(), ^{

        // configure scrollView and imageView
        CGSize photoSize = self.imageView.image.size;

        self.scrollView.delegate = self;
        [self.scrollView setZoomScale:1.00];
        [self.scrollView setMinimumZoomScale:0.5];
        [self.scrollView setMaximumZoomScale:1.00];
        self.scrollView.contentSize = photoSize;

        self.imageView.frame = CGRectMake(0, 0, photoSize.width, photoSize.height);

        CGPoint scrollCenter = self.scrollView.center;
        self.imageView.center = scrollCenter;
    });
});
dispatch_release(downloadQueue);
Run Code Online (Sandbox Code Playgroud)

并且在主视图中选择图片时,我设置了detailView(scrollView)的属性photoURL,所以我覆盖了photoURL的setter方法

-(void) setPhotoURL:(NSURL *)photoURL
{
    // do something to refresh my scrollView, below are something I already tried
    self.imageView.image =  [UIImage imageWithData: [NSData dataWithContentsOfURL:self.photoURL]];
    [self.scrollView setNeedsDisplay];
    [self.view setNeedsDisplay];
    [self.imageView setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)

我的目的是当我的模型(photoURL)给出时,我的scrollView应该给我相应的imageView,但是当我的应用程序运行并且我选择了一张照片时,scrollView没有"更新",我已经学会了如何更新UITableView(self .tableView reloadData),但我真的不知道怎么做,请帮忙!

PS:

1请原谅我可怜的英语-_-我已尽力解释清楚.

2我没有足够的声誉来更新图像让你们完全理解我的应用程序是什么,希望你能理解我所说的内容


更新:我使用KVO机制而不是覆盖photoURL的setter方法,但[self.scrollView setNeedsDisplay]仍然不起作用.

我使用调试器来跟踪进度,KVO可以工作,但是[self.xxx setNeedsDisplay]后没有任何事情发生,我完全糊涂了,不知道该怎么做.

在此输入图像描述

Ric*_*III 1

您实际上应该使用 KVO,而不是覆盖设置器:

-(void) viewDidLoad
{
    ...
    [self addObserver:self forKeyPath:@"photoURL" options:0 context:NULL];
    ...
}

-(void) observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
    if ([keyPath isEqualToString:@"photoURL"])
    {
        self.imageView.image =  [UIImage imageWithData: [NSData dataWithContentsOfURL:self.photoURL]];
        [self.scrollView setNeedsDisplay];
        [self.view setNeedsDisplay];
        [self.imageView setNeedsDisplay];
    }
}
Run Code Online (Sandbox Code Playgroud)

并记住@synthesize你的财产!