我需要在UICollectionView
的单元格中显示相同的图像,但具有以下逻辑:
例如:
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 :(得分: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 :(得分: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