前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS自定义UICollectionView和UITableView单元格选中样式

iOS自定义UICollectionView和UITableView单元格选中样式

作者头像
陈满iOS
发布2018-09-10 11:09:04
3K0
发布2018-09-10 11:09:04
举报
文章被收录于专栏:陈满iOS

iOS中,UICollectionView和UITableView已经有系统默认选中颜色设置,但是只有无色,蓝色,灰色,三种颜色设置,如果想要其他的颜色效果,我们可以自由自定义设置。

前言

  • 先观赏一下典型的UITableView控件案例

image.png

  • 典型的UICollectionView控件案例

image.png

1.单元格默认选中效果

  • 系统默认单元格选中样式
代码语言:javascript
复制
//无色
cell.selectionStyle = 
UITableViewCellSelectionStyleNone
;
//蓝色
cell.selectionStyle = 
UITableViewCellSelectionStyleBlue
;
//灰色
cell.selectionStyle = 
UITableViewCellSelectionStyleGray
;
  • 系统默认单元格样式(无选中效果)
代码语言:javascript
复制
cell.selectionStyle = UITableViewCellStyleDefault;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;

示例

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyTableViewCell class]) owner:self options:nil] lastObject];
    }
    cell.cellMdl = [self.tableItemArr objectAtIndex:indexPath.row];
    
    //设置选中背景色
    cell.selectionStyle = UITableViewCellStyleDefault;
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}

2.单元格自定义选中效果方案(一)

  • 通用方案:

假设你已经正确实现其他代理方法,需要在table或collection的返回cell的代理方法中作如下设置:

代码语言:javascript
复制
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];

示例:

  • UICollectionView
代码语言:javascript
复制
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    return [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCellId" forIndexPath:indexPath];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellId forIndexPath:indexPath];
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
////    错误的做法:当次级VC返回时才会调用
//    if (cell.isHighlighted) {
//        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    }else{
//        cell.backgroundColor = [UIColor whiteColor];
//    }
    
    return cell;
}
  • UITableView
代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AssistantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([AssistantTableViewCell class]) owner:self options:nil] lastObject];
    }
    cell.cellMdl = [self.questionItemArr objectAtIndex:indexPath.row];
    
    //设置选中背景色
//    cell.selectionStyle = UITableViewCellStyleDefault;
//    cell.selectionStyle = UITableViewCellSelectionStyleDefault;

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    return cell;
}

3.单元格自定义选中效果方案(二)

  • 通用方案:
  • [x] 在自己自定义的cell文件中重写如下方法:

示例:

  • UITableViewCell.m
代码语言:javascript
复制
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted];
    if (highlighted) {
        //选中时
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }else{
        //非选中
        self.backgroundColor = [UIColor whiteColor];
    }
}
  • UICollectionView.m
代码语言:javascript
复制
- (void)setHighlighted:(BOOL)highlighted{
    [super setHighlighted:highlighted];
    if (highlighted) {
        //选中时
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }else{
        //非选中
        self.backgroundColor = [UIColor whiteColor];
    }
}

4.小结

如你所见,不难发现,两个cell设置套路是一样的(捂脸)。

注意的是,方案一和方案二不要重复设置。另外,二者择一的话,推荐方案一。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.06.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 1.单元格默认选中效果
  • 2.单元格自定义选中效果方案(一)
  • 3.单元格自定义选中效果方案(二)
  • 4.小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档