iOS-Cell单选的3中方法(互斥事件)

本文仅提供Cell互斥的三种思路,代码变换无穷,可以灵活运用到自己的项目中

效果图

第一种方法:点击Cell互斥

使用系统方法:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
//此方法点击Cell时,会使离开Cell的selected = NO,点击Cell的selected = YES

使用:

#import "CustomSingleCell.h"
@implementation CustomSingleCell
{
UIButton * button;
}
- (void)awakeFromNib {
[super awakeFromNib];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    self.accessoryType = UITableViewCellAccessoryNone;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    button = [[UIButton alloc]init];
    [self.contentView addSubview:button];
    button.frame = CGRectMake(10, 10, 100, 30);
    [button setTitle:@"未选中" forState:UIControlStateNormal];
    [button setTitle:@"选中" forState:UIControlStateSelected];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)onClick:(UIButton *)btn{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
/*在此处设置*/
button.selected =  selected;
}

第二种方法:点击Button互斥

#import <UIKit/UIKit.h>
typedef void (^CustomButtonSingleOneCellBlock)();
@interface CustomButtonSingleOneCell : UITableViewCell
@property(nonatomic,copy)CustomButtonSingleOneCellBlock SingleBtnBlock;
@property(nonatomic,strong) UIButton * button;;
@end

#import "CustomButtonSingleOneCell.h"
@implementation CustomButtonSingleOneCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    self.backgroundColor = [UIColor whiteColor];
    self.accessoryType = UITableViewCellAccessoryNone;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.button = [[UIButton alloc]init];
    [self.contentView addSubview:self.button];
    self.button.frame = CGRectMake(10, 10, 100, 30);
    [self.button setTitle:@"未选中" forState:UIControlStateNormal];
    [self.button setTitle:@"选中" forState:UIControlStateSelected];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)onClick:(UIButton *)btn{
self.SingleBtnBlock(btn);
}

ViewController使用

@implementation LPSingleViewController
{
UIButton *tempBtn;
}

CustomButtonSingleOneCell *cell =[tableView dequeueReusableCellWithIdentifier:@"CustomButtonSingleOneCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.button.selected = YES;
    }
    cell.SingleBtnBlock = ^(UIButton *btn){
        tempBtn.selected = NO;
        tempBtn = btn;
        btn.selected = YES;
    };
    return cell;

第三种方法:点击Button或者Cell互斥、本文提供点击Cell原理相同

#import <UIKit/UIKit.h>
@interface CustomBtnSingleTwoCell : UITableViewCell
@property(nonatomic,assign)BOOL isBtnSelected;
- (void)setBtnSelected;
@end
#import "CustomBtnSingleTwoCell.h"
@implementation CustomBtnSingleTwoCell{
UIButton * button;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    self.accessoryType = UITableViewCellAccessoryNone;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    button = [[UIButton alloc]init];
    [self.contentView addSubview:button];
    button.frame = CGRectMake(10, 10, 100, 30);
    [button setTitle:@"未选中" forState:UIControlStateNormal];
    [button setTitle:@"选中" forState:UIControlStateSelected];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
}
return self;
}
- (void)setBtnSelected{
button.selected = self.isBtnSelected;
}
@end

ViewController使用

boolArray = [NSMutableArray arrayWithArray:@[@(YES),@(NO),@(NO),@(NO),@(NO)]] ;
CustomBtnSingleTwoCell *cell =[tableView dequeueReusableCellWithIdentifier:@"CustomBtnSingleTwoCell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor yellowColor];
    cell.isBtnSelected = [boolArray[indexPath.row] boolValue];
    [cell setBtnSelected];
    return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 2) {
    
    //点击的时候确定点击状态。最好有model设置
    [boolArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == indexPath.row) {
            boolArray[indexPath.row] = @(YES);
        }else{
            boolArray[idx] = @(NO);
        }
    }];
    
    [self.tableView reloadSections:[[NSIndexSet alloc]initWithIndex:2] withRowAnimation:UITableViewRowAnimationFade];
}
}

源码下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,512评论 4 61
  • 1. 在商场里排队买冰淇淋,走过来一个孕妇,孕妇跟她旁边的男人说:“老公,我想吃冰淇淋”,男人走过去问了价,十五块...
    聪慧的女孩子阅读 3,244评论 0 0
  • 你们好(当你们看见这篇文章时),礼貌起见,我应该先作下自我介绍。(我已经作过很多了,但我不能保证我的每篇文章都被你...
    4f427883ff9e阅读 5,480评论 4 5
  • 一、“检视阅读”—了解本章节的作用、了解本章节大致内部结构 1、本章的目的:意志力是否可以培养—全书中起到承上启下...
    venicate阅读 3,514评论 0 0
  • 一男的深夜遇到一美女,美女说理由a,男便带女回家,xxoo中女的一个回旋踢用高跟鞋刺破男的脑袋,附身在男的身上,
    fffaaa阅读 3,576评论 0 0

友情链接更多精彩内容