2014-09-10 66 views
1

的細胞相同的圖像,我需要表現出對UICollectionView細胞相同的圖像,但具有以下邏輯:顯示在UICollectionView

  • 我有14張不同的背景圖片
  • 我需要重複相同的影像每個細胞,其是14.

例如多發性:

indexPath.row == 1 , 
indexPath.row == 14, 
indexPath.row == 28 

等,放一個圖像

indexPath.row == 2 , 
indexPath.row == 15, 
indexPath.row == 29 

設置另一個圖像,依此類推。

如何解決此請求?

我已經嘗試過這樣的代碼,但似乎沒有成功:(

- (void)setImageForCell:(UICollectionViewCell *)curCell forIndexPath:(NSIndexPath *)indexPath 
{ 
//=> Get service image 
UIImageView *imgService = (UIImageView *)[curCell viewWithTag:101]; 

for (NSUInteger i = 0; i < 14; i++) 
{ 
    if (i == indexPath.row && indexPath.row % 13 == 0) 
    { 
     imgService.image  = [UIImage imageNamed:[NSString stringWithFormat:@"service_%d" , i]]; 
    } 
} 
} 

感謝

+0

解決辦法其實很簡單。但是,如果你遇到問題,你不應該先嚐試一下併發佈一個問題,而不是要求某人給你一個答案嗎? – Rick 2014-09-10 14:22:00

+2

您的請求是關於加載圖像,還是關於爲該行選擇合適的圖像?如果是後者,請檢查模運算符%。 (indexPath.row%14)將在除以14之後返回「餘數」。然後,您可以將其用作圖像數組的索引。 – pbasdf 2014-09-10 14:22:04

+0

@Rick:我更新的代碼是什麼我已經試過,但沒有成功 – Bonnke 2014-09-10 14:41:16

回答

1

試試這個:

if(indexPath.row < 14) 
{ 
    imgService.image = [UIImage imageNamed:[NSString stringWithFormat:@"service_%d" , indexPath.row]]; 
} 
else 
{ 
    if (indexPath.row % 14 == 0) 
    { 
     for (NSUInteger i = 0; i < 14; i++) 
     { 
      imgService.image = [UIImage imageNamed:[NSString stringWithFormat:@"service_%d" , i]]; 
     } 
    } 
} 

imgService.image = [UIImage imageNamed:[NSString stringWithFormat:@"service_%d" , indexPath.row % 14]]; 
1

試試這個:

- (void)setImageForCell:(UICollectionViewCell *)curCell forIndexPath:(NSIndexPath *)indexPath 
{ 
    // I would prefer to subclass the cell instead of using tag 
    UIImageView *imgService = (UIImageView *)[curCell viewWithTag:101]; 
    imgService.image = [UIImage imageNamed:[NSString stringWithFormat:@"service_%d.png", indexPath.row % 14]]; 
} 

(注意:我在Xcode之外輸入了這個內容,我假設你的圖像以service_0.png開頭。如果沒有,就相應調整。)

順便說一句,

indexPath.row == 0, <-- should start with 0 instead of 1 
indexPath.row == 14, 
indexPath.row == 28 
... 
indexPath.row == 1, <-- should be 1 
indexPath.row == 15, 
indexPath.row == 29 
相關問題