相关文章推荐
气势凌人的小刀  ·  011 Error:Failed to ...·  4 月前    · 
捣蛋的眼镜  ·  【微机原理】实验五 ...·  1 年前    · 
害羞的帽子  ·  linux基础命令:md5sum - ...·  1 年前    · 
阳刚的伏特加  ·  react 使用 use-immer ...·  1 年前    · 


效果

  • 单选
  • iOS 选择题之TableView单选与多选的处理_数据模型

  • 多选
  • iOS 选择题之TableView单选与多选的处理_数据模型_02


方法

重写 ​ ​UITableViewCell​ ​​的 ​ ​- (void)setSelected:(BOOL)selected animated:(BOOL)animated{}​ ​ 方法

  • 系统会自动处理​ ​cell​ ​ 的选中与取消,这个可以用来处理​ ​单选​
  • 对于​ ​多选​ ​,针对之前的选中状态加以判断即可
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

if (_multiChoose) { // 多选

// 选中 cell ,并且 之前 未选中 cell,选中它!!!
if (selected && !_chooseButton.selected){
_answerLabel.textColor = UIColorFromHexRGB(0xfd9748);
_chooseButton.selected = YES;
}

// 取消选中 cell ,并且 之前 选中 cell
else if (!selected && _chooseButton.selected){
// 不处理
}

// 选中 cell ,并且 之前 选中 cell,取消选中!!!
else if (selected && _chooseButton.selected) {
_answerLabel.textColor = kDetailTextColor;
_chooseButton.selected = NO;
}

}else{ // 单选

UIColor *color;
if (selected) { // 选中
color = UIColorFromHexRGB(0xfd9748);
}else{ // 取消
color = kDetailTextColor;
}
_answerLabel.textColor = color;
_chooseButton.selected

注意

要结合​ ​数据模型​ ​​进行判断,滚动​ ​TableView​ ​​, ​ ​cell​ ​​移出屏幕时,会调用上面的方法,会导致最后选择的一个​ ​cell​ ​被取消。

也可以禁止​ ​TableView​ ​滚动。