2014-09-10 69 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 
相关问题