简体   繁体   English

选中后如何更改集合视图单元格的背景颜色?

[英]how to change collection view cell background colour when it is selected?

This code is I used if cell is selected then the background colour have to change in image view it is placed inside the collection view cell. 如果选择了单元格,则使用此代码,然后必须在图像视图中更改背景色,将其放置在集合视图单元格内。 But its not working 但是它不起作用

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  {
      if (cell.selected) {
        cell.img_cell.backgroundColor = [UIColor colorFromHexString:@"#ffc400"]; // highlight selection
      }
      else
      {
        cell.backgroundColor = [UIColor clearColor]; // Default color
      }
      NSLog(@"Selected section>> %@",[arr_images objectAtIndex:indexPath.row]);

      //    cell.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];
  }

now its working i removed the if condition and tried using cellForItemAtIndexPath . 现在,它的工作我删除了if条件,并尝试使用cellForItemAtIndexPath。

cell = [collectionView cellForItemAtIndexPath:indexPath];

        cell.img_cell.backgroundColor = [UIColor colorFromHexString:@"#ffc400"]; // high

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择 UICollectionView 单元格时更改背景和 label 颜色 - Change background and label colour of UICollectionView cell when it is selected 如何更改 UICollectionView 中所选项目的单元格背景颜色,并将其他单元格颜色更改为默认值 swift - how to change cell background colour on selected item in UICollectionView and change other Cell colour to default in swift 如何通过单击更改集合视图单元格中的标签颜色 - how to change the label colour in collection view cell by click 使用UIAppearance更改选定的单元格背景颜色 - Change the selected cell background colour using UIAppearance 更改单元格的背景颜色 - Change background colour of Cell 在iOS中具有背景颜色的收藏夹视图单元上添加图像 - Add an image on collection view cell having background colour in iOS 如何在Swift iOS中更改自定义“收藏夹视图”单元格的背景颜色? - How to change the background color of a custom Collection View cell in swift iOS? 如何更改选定的可扩展表格视图单元格的背景颜色 - How to change the background color of selected expandable table view cell 您好,我正在使用 swift 并且我正在尝试在选择集合视图单元格时更改集合视图单元格的 label - Hello, I am using swift and I am trying to change a label of a collection view cell when the a collection view cell is selected 集合视图选定的单元格背景视图颜色在 IOS 15 中未更改 - Collection view selected cell background view color not changing in IOS 15
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM