iOS collectionViewCell显示选中颜色

demo

git地址:点这里

原理思想简介
  1. reloadData只会刷新内存中保存的cell,注意cell会复用,所以最多会刷新当前显示的加上一个用来复用的cell。
  2. 实现的思想有两种:
  • 设置一个全局的变量保存选中的位置,然后刷新collectionView,在返回cell的方法中判断是不是选中的cell,然后显示对应的颜色。
  • 在model中设置一个颜色的属性,每次对应的修改model的属性值,然后刷新collectionView就可以实现选中颜色。
主要代码(此处只展示第一种实现方式)
复制代码
 - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
StyleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemResuableStr forIndexPath:indexPath];

//判断显示颜色
UIColor *tempColor = (self.selectIndex == indexPath) ? [UIColor redColor] : [UIColor groupTableViewBackgroundColor];
[cell setItemWithDic:self.styleInfoMuArray[indexPath.item] BackColor:tempColor];
return cell;
}

 #pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

//这句是无关的,请忽略
//设置大图
self.originalImage.image = [self.styleInfoMuArray[indexPath.item] objectForKey:StyleImage];

//设置选中位置
self.selectIndex = indexPath;

//刷新collectionView
[self.selectStyleCollectionView reloadData];
}
相关推荐
2601_9557674211 小时前
iPhone 17 全系屏幕光学特性与保护膜选型技术分析
ios·ar·iphone·圆偏振光护眼·iphone17护眼钢化膜·#观复盾护景贴
2501_9159090612 小时前
深入解析Mock.js:功能、应用及实战案例,提升前端开发效率
android·ios·小程序·https·uni-app·iphone·webview
pop_xiaoli15 小时前
【iOS】通知传值实现
macos·ios·xcode
2601_9557674215 小时前
iPhone 17 屏幕偏振光学解析与保护贴技术选择——悟赫德观复盾护景贴
人工智能·科技·ios·ar·iphone·圆偏振光
qq36219670516 小时前
手机App下载安装完全指南:2026最新教程(Android & iOS)
android·ios·智能手机
核电机组16 小时前
IOS原生APP集成Flutter
flutter·ios
亘元有量-流量变现17 小时前
预算有限怎么投?iOS ASO与ASA优劣对比及精细化联动增长策略
ios·用户运营·aso优化·亘元有量·方糖试玩
Mr -老鬼18 小时前
EasyClick iOS自动化7.1.0新版本详解
ios·自动化·easyclick
2501_9160074719 小时前
不用 Mac 也可以 Windows下管理iOS描述文件的非Xcode完整指南
android·ios·小程序·https·uni-app·iphone·webview