iOS UICollectionViewCell点击改变背景图片

释放双眼,带上耳机,听听看~!

方法一:在控制器里面进行修改:

代理方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
cell.discoverRechangeCenterModel = self.sourceArr[indexPath.row];
//默认选中效果
if (indexPath == self.selectIndexPath) {
cell.backgroundImgV.hidden = NO;
} else {
cell.backgroundImgV.hidden = YES;
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
DiscoverRechangeCenterCell *cell = (DiscoverRechangeCenterCell *)[collectionView cellForItemAtIndexPath:indexPath];
//选中之后的cell变颜色
[self updateCellStatus:cell selected:NO];
self.selectIndexPath = indexPath;
[collectionView reloadData];
}
//取消选中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
DiscoverRechangeCenterCell *cell = (DiscoverRechangeCenterCell *)[collectionView cellForItemAtIndexPath:indexPath];
[self updateCellStatus:cell selected:YES];
}
// 改变cell的背景图片
-(void)updateCellStatus:(DiscoverRechangeCenterCell *)cell selected:(BOOL)selected{
cell.backgroundImgV.hidden = selected;
}

方法二:在UICollectionViewCell中重写select方法实现

UICollectionViewCell中:
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
self.backgroundImgV.hidden = NO;
} else {
self.backgroundImgV.hidden = YES;
}
}
控制器中:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
cell.discoverRechangeCenterModel = self.sourceArr[indexPath.row];
//默认选中效果
if (indexPath == self.selectIndexPath) {
cell.backgroundImgV.hidden = NO;
} else {
cell.backgroundImgV.hidden = YES;
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
DiscoverRechangeCenterCell *cell = (DiscoverRechangeCenterCell *)[collectionView cellForItemAtIndexPath:indexPath];
self.selectIndexPath = indexPath;
[cell setSelected:YES];
[collectionView reloadData];
}

人已赞赏
iOS文章

去除Xcode警告 - Dsymutil Warning

2020-1-1 5:55:28

iOS文章

iOS UIViewController的属性

2020-1-1 8:51:04

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索