【问题标题】:Add an image on collection view cell having background colour in iOS在 iOS 中具有背景颜色的集合视图单元格上添加图像
【发布时间】:2016-09-14 05:52:32
【问题描述】:

我正在开发我的应用程序,在该应用程序中,我必须在选择时将图像添加到集合视图单元格。集合视图单元格是圆形的并填充有颜色,因此在选择单元格时,刻度的图像应该在该颜色上。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {        
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

NSArray *hexaColor= _advantageModelObj.productDetail.carColorDetails;
    CarColorDetailObject *carColor= [hexaColor objectAtIndex:indexPath.row];

NSString *hexCode=  carColor.carColor.hexCode;                            
cell.layer.masksToBounds=TRUE;       
cell.backgroundColor=[self colorWithHexString:hexCode];       
cell.layer.cornerRadius= (roundf(cell.frame.size.width/2));        

cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"colorselected.png"]];

    return cell;        
}

【问题讨论】:

  • 向我们展示您尝试过的代码。
  • 这里也显示一些屏幕截图以了解您的需求
  • 所以我有圆形单元格的集合视图,所以从 api 我已经将颜色填充到单元格,所以现在单击该单元格时,必须将显示其选定的图像添加到单元格中,因此背景颜色也是可见

标签: ios collections view


【解决方案1】:
  1. 制作全局变量

int clickedIndex;

viewDidLoad 中的 2 分配给 clickedIndex

clickedIndex=-1;
  1. 在collectionView中didSelectItemAtIndexPath

    clickedIndex=(int)indexPath.row;
    [collectionView reloadData];
    

4 在collectionView cellForItemAtIndexPath 中添加一些行

if(clickedIndex==indexPath.row){


UIImageView* imageView=[[UIImageView alloc]init];
[imageView setFrame:CGRectMake(10, 0, cell.contentView.size.width, cell.contentView.size.height)];
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@“tickImage”,]];
imageView.alpha=0.5;

[cell.contentView addSubview: imageView];



    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-20
    • 2020-11-28
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多