2013-07-27 173 views
1

我在我的Xcode項目中有兩個for循環用於更改9個UIImageView中的圖像。這些UIImageView中的圖像是從服務器下載並呈現的。簡單For循環問題

我的for循環使用3個不同的整數來確定要顯示的圖像: next,current_photo和previous是整數。

我有兩個UIButtons控制顯示下一個和上一組圖像。

當我想顯示下一個圖像集,我提出和使用循環如下:

NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9]; 

     for (next = current_photo; next < (current_photo+9); next++) { 
      NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, next+1, FORMAT_TYPE]; 
      NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]]; 
      UIImage *image = [[UIImage alloc] initWithData:imageData]; 
      [[imageViews objectAtIndex:(next-9)] setImage:image]; 
     } 

     current_photo = next; 

for循環完美的作品和所有9個UIImagesViews改變形象。

然而,當我想表明在9個UIImageViews的前一組圖片,下面的for循環不會因爲某些原因正常工作:

NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9]; 

     for (previous = current_photo; previous > (previous-9); previous--) { 
      NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, previous+1, FORMAT_TYPE]; 
      NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]]; 
      UIImage *image = [[UIImage alloc] initWithData:imageData]; 
      [[imageViews objectAtIndex:(previous-previous)] setImage:image]; 
     } 

     current_photo = previous; 

有什麼不對我的for循環?請解釋。

下面是我的應用程序打開時會發生什麼: Opened App

下面是當按下一個按鈕會發生什麼:按後退按鈕的應用程序只是當

Next

最後凍結.....

爲什麼?怎麼了?請幫忙。

感謝您的時間:)

回答

1

一兩件事:

[[imageViews objectAtIndex:(previous-previous)] setImage:image]; 

將始終以[0],並沒有其他人就會有什麼對他們設置圖像設置。

而且

(previous = current_photo; previous > (previous-9); previous--) 

將永遠循環下去!每次你做一個以前的事情 - 你正在比較的東西知道什麼時候停止,前一個9也會下降。

我會推薦這:

for (previous = current_photo; previous > (current_photo-9); previous--) { 
    NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, previous+1, FORMAT_TYPE]; 
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]]; 
    UIImage *image = [[UIImage alloc] initWithData:imageData]; 
    [[imageViews objectAtIndex:(8 - (current_photo - previous))] setImage:image]; 
} 
+0

好一點。但問題是,我無法弄清楚用什麼來替代它。我如何告訴Xcode我需要它來替換每個圖像。 – Supertecnoboff

+0

謝謝。我試過你的循環,但它不起作用。原因是我的NSArray有8個項目。你的for循環想寫到第九個不存在的...... :( – Supertecnoboff

+0

你有9個項目在你的數組中,我錯誤地使用了9 - (current_photo - previous),它應該是8.因此第一個時間通過,它會寫入imageViews [8],最後一次通過它將寫入imageViews [0] – HalR