iOS菜鸟级开发IOS 视图跳转/层级相关的知识iOS_Skill_Collect

iOS Cell高亮(highlighted)和选中(selec

2017-01-17  本文已影响5532人  断忆残缘

前言

初衷:最近闲适,讲讲关于Cell,也希望刚入门的iOS小伙伴少走写弯路吧!

UITableViewUICollectionView是我们常用的控件。

但是,就关于如何设置高亮状态选中状态,网上的解决方案虽多,但是令我满意的却是寥寥无几。

在我尝试各种方案后,终于在官方API中找到了解决方案。

正文

一. 阐释

二. 选中状态(selected)

1. 单选

默认状态下,当你单击一个Cell,它就会变成灰色,这个表明你已经选中了这个Cell,选中效果如下:

signle.gif
2. 多选

有的小伙伴既然可以单选,那么多选么?
当然iOSAPI很人性化,设置多选只需一行代码,方法如下:

    // 设置允许多选
    self.tableView.allowsMultipleSelection = YES;

多选效果如下:

multiple.gif
3. 获取单选和多选的选中项
// 获取单选选中项
NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
// 获取全选选中项
NSArray *indexPaths = self.tableView.indexPathsForSelectedRows;
4. 设置Cell选中和反选中
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// 设置某项变为选中
[self.tableView selectRowAtIndexPath:indexPath animated:YES
                      scrollPosition:UITableViewScrollPositionNone];
// 设置某项变为未选中
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 在手指离开的那一刻进行反选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
deSelect.gif

当然这个效果的确在一段时间内帮我解决了不少问题吧,知道我遇见了“她”,她让我在度陷入“懵逼”的状态。接下来我我们就来见识见识这个让我懵逼的“她”。

background.png backgroundColor.gif
// 禁用SelectionStyle
self.selectionStyle = UITableViewCellSelectionStyleNone;
  1. UIColor -> UIImage,使用UIImage填充,不过也有短处。
// animate between regular and selected state
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;   
// animate between regular and highlighted state                  
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated;

UITableViewCell方法
建议:最好增加延迟消失的动画,让高亮效果更加突现出来。否则短暂点击无法显示出高亮效果,原因是高亮与非高亮状态切换太快无法显示其效果。

// 禁用SelectionStyle
self.selectionStyle = UITableViewCellSelectionStyleNone;

// 配置cell高亮状态
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
        self.contentView.backgroundColor = [UIColor colorWithHexString:@"0xcccccc"];
    } else {
        // 增加延迟消失动画效果,提升用户体验
        [UIView animateWithDuration:0.1 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.contentView.backgroundColor = [UIColor whiteColor];
        } completion:nil];
    }
}

// 配置cell选中状态
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        self.contentView.backgroundColor = [UIColor colorWithHexString:@"0xececec"];
    } else {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
}

UICollectionViewCell方法
由于UICollectionViewCell没有像UITableViewCell中那样的方法,所以重写setter方法即可,具体代码如下:

Objective-C版代码
// 设置高亮效果
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    if (highlighted) {
        self.backgroundColor = HexRGB(0xececec);
    } else {
        [UIView animateWithDuration:0.1 delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.backgroundColor = [UIColor whiteColor];
        } completion:nil];
    }
}

// 设置选中选中
-(void)setSelected:(BOOL)selected {
    if (selected) {
        self.backgroundColor = HexRGB(0xececec);
    } else {
        self.backgroundColor = [UIColor whiteColor];
    }
}

Swift版代码
  // 设置高亮效果
  override var isHighlighted: Bool {
      willSet {
          if newValue {
              
          } else {
              
          }
      }
  }

  // 设置选中选中
  override var isSelected: Bool {
      willSet {
          if newValue {
              
          } else {
              
          }
      }
  }

本人不足之处欢迎大家指正,谢谢!

上一篇下一篇

猜你喜欢

热点阅读