美文网首页IOS收藏
CollectionView点击cell变色

CollectionView点击cell变色

作者: 陈大帅 | 来源:发表于2016-03-23 09:23 被阅读1460次

在// cell显示内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

NSArray *arr = [NSArray arrayWithObjects:@"今天", @"明天", @"后天", nil];

DayBtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dayBtnCell" forIndexPath:indexPath];

cell.lab.text = arr[indexPath.row];

// cell点击变色

UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.bounds];

selectedBGView.backgroundColor = kMainColor;

cell.selectedBackgroundView = selectedBGView;

return cell;

// cell点击变色

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

// cell点击变色

- (void)collectionView:(UICollectionView *)colViewdidUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath

{

if ([colView isEqual:self.dayCollection]) {

DayCollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];

[cell setBackgroundColor:UIColorFromRGB(0xE6E6E6)];

} else {

DayBtnCollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];

[cell setBackgroundColor:UIColorFromRGB(0xC8C8C8)];

}

}

相关文章

网友评论

  • 酒红色T恤:Demo发一下,谢了,1298108889@qq.com
  • 酒红色T恤:我的项目中cell添加了一个label,占满cell。
    现在需要cell在点击按下时,背景变为蓝色,松开后,恢复原来背景。
    其实是改变label的颜色和label字体的颜色

本文标题:CollectionView点击cell变色

本文链接:https://www.haomeiwen.com/subject/hlmllttx.html