2012-03-23 49 views
5

我是iOS編程新手,對iOS和Objective-C知識有限,請給我一些幫助。iOS如何在模型更改時刷新ScrollView?

我正在開發一個flickr照片應用程序,它基本上需要來自flickr API的照片數據並在scrollView中顯示。

但我在我的iPad版本中遇到了一些問題,用UISplitViewController提供。 讓我給我一個簡單的介紹我的應用程序。

MASTER VIEW:嵌入UINavigationViewController,基本上它只是使用一些Flickr的API來獲取一些數據,目前像這樣的UITableViewController的列表:

navigationController - >

tableViewControllerA(由國家組織的照片) - >

tableViewControllerB(在該國照片列表) 當TableViewControllerB被選擇的照片,在我的代碼我通過照片URL(從Flickr API)到我的詳細視圖控制器,其是滾動型,這裏是我的代碼在滾動lView的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); 

和在主視圖當選擇了圖像,我設置的DetailView(滾動視圖)的財產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]; 
} 

我的目的只是當我的模型(photoURL)給,我scrollView應該給我相應的imageView,但是當我的應用程序運行,我選擇了一張照片,scrollView沒有「更新」,我已經學會了如何更新UITableView(self.tableView reloadData),但我真的不知道該怎麼做,請幫忙!

PS:

1請原諒我的英語太差-_-我已經試圖解釋儘可能明確。

2我沒有足夠的信譽來更新圖像,讓你們完全理解我的應用程序是什麼,希望你可以通過剛纔我說


UPDATE明白: 我用KVO機制,而不是覆蓋了photoURL的setter方法,但[self.scrollView setNeedsDisplay]仍然不起作用。

我使用調試器來跟蹤進度,KVO的作品,但[self.xxx setNeedsDisplay]後,沒有發生任何事情,我完全困惑,不知道該怎麼做。

enter image description here

回答

1

你實際上應使用國際志願者組織,不重寫setter方法:

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

要記住,你的@synthesize性能!

+0

首先感謝您的幫助,我還沒有學會KVO。我將閱讀Apple文檔指南,然後測試它是否可行。謝謝〜 – ifournight 2012-03-23 14:29:18

+1

...並記住removeallObserver在dealloc .... – bandejapaisa 2012-03-23 14:31:14

+0

Em ... KVO機制工作正常,但[self.scrollView setNeedsDisplay]仍然沒有任何影響... – ifournight 2012-03-23 14:46:28

0

調用[self.view setNeedsLayout]而不是setNeedsDisplay。 這將刷新視圖的所有子視圖,包括scrollView。

0

我有Swift版本,不知道它是否適用於您,但它對我有用。

  1. 只是寫在viewDidLayoutSubviews()你的代碼像下面

    override func viewDidLayoutSubviews() { 
    self.scroller.contentSize = CGSize(width: self.view.frame.width, 
    height: self.websiteLabel.frame.maxY + 20) 
    } 
    
  2. 而且在viewDidAppear()像調用此方法...

    override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    viewDidLayoutSubviews()  
    } 
    

希望這將幫助你們..

相關問題