tableView 点击cell弹出确认框,点确认cell变成选中状态,否则选择上一个cell

 

//
//  WYPPointBusinessLineCell.h
//  JH-POS-Platform
//
//  Created by 王彦平 on 2020/8/31.
//  Copyright © 2020 JH. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "WYPElectionBusinessModel.h"

NS_ASSUME_NONNULL_BEGIN
typedef void(^SelectCellBlock)(void);

@interface WYPCustomSelectLeftListViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *leftLine;

@property (weak, nonatomic) IBOutlet UILabel *linee;

@property (weak, nonatomic) IBOutlet UILabel *lineNameLabel;

@property (nonatomic,strong) UIColor         *defaColor;

@property (nonatomic,assign) BOOL            isChangeLibrary;//是否是转库变更
@property (nonatomic,assign) BOOL            isSureChoose;//是否已经确认选中

@property (nonatomic,copy) SelectCellBlock selectCellBlock;

+(instancetype)WYPCustomSelectLeftListViewCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath;


@end

NS_ASSUME_NONNULL_END
//
//  WYPPointBusinessLineCell.m
//  JH-POS-Platform
//
//  Created by 王彦平 on 2020/8/31.
//  Copyright © 2020 JH. All rights reserved.
//

#import "WYPCustomSelectLeftListViewCell.h"

@interface WYPCustomSelectLeftListViewCell()

@property (strong, nonatomic) WYPElectionBusinessModel *model;

@end
@implementation WYPCustomSelectLeftListViewCell

+(instancetype)WYPCustomSelectLeftListViewCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    NSString *cellId = @"WYPCustomSelectLeftListViewCell";
    WYPCustomSelectLeftListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"WYPCustomSelectLeftListViewCell" owner:nil options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    if (selected) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.leftLine.hidden = NO;
        self.lineNameLabel.attributedText = [Tools attributedTextArray:@[self.lineNameLabel.text] textColors:@[[UIColor colorWithHexString:@"#FFB126"]] textfonts:@[Font(11)] lineSpacing:1];
        self.lineNameLabel.font = FontAndName(@"PingFangSC-Semibold", 11);
    }else{
        
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        self.leftLine.hidden = YES;
        self.lineNameLabel.attributedText = [Tools attributedTextArray:@[self.lineNameLabel.text] textColors:@[[UIColor colorWithHexString:@"#666666"]] textfonts:@[Font(11)] lineSpacing:1];
        self.lineNameLabel.font = FontAndName(@"PingFangSC-Regular", 11);
    }
}

@end

//
//  WYPPointBusinessLineView.h
//  JH-POS-Platform
//
//  Created by 王彦平 on 2020/8/31.
//  Copyright © 2020 JH. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@class WYPDatamodel;

typedef void(^SelectTabelViewIndexBlock)(WYPDatamodel *model,NSIndexPath *indexPath);

typedef void(^AllBusinessBtnClickedBlock)(NSString *str);

@interface WYPCustomSelectLeftListView : UIView

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSMutableArray             *dataArray;
@property (nonatomic,copy) SelectTabelViewIndexBlock    selectBlock;//cell点击
@property (nonatomic,copy) AllBusinessBtnClickedBlock   allBtnBlock;//全业务线按钮

@property (nonatomic,assign) BOOL                       isHaveAllBusinessBtn;//是否有全业务线
@property (nonatomic,assign) BOOL                       isHaveAllBusinessDefaultSellect;//在有全业务线状态下,有默认选择值
@property (nonatomic,assign) BOOL                       isChangeLibrary;//是否是转库变更
@property (nonatomic,assign) BOOL                       isNoSureChoose;//是否已经确认选中(NO为选中)

@property (nonatomic,assign) NSInteger                  inSection;//默认选中组
@property (nonatomic,assign) NSInteger                  isRow;//默认选中行
@property (nonatomic,strong) NSIndexPath                *indexPath;

@property (nonatomic,copy) NSString *productId;
@property (nonatomic,assign) NSInteger count;


+(instancetype)WYPCustomSelectLeftListView;


@end

NS_ASSUME_NONNULL_END
//
//  WYPPointBusinessLineView.m
//  JH-POS-Platform
//
//  Created by 王彦平 on 2020/8/31.
//  Copyright © 2020 JH. All rights reserved.
//

#import "WYPCustomSelectLeftListView.h"
#import "WYPCustomSelectLeftListViewCell.h"
#import "WYPDatamodel.h"
#import "WYPElectionBusinessModel.h"
#import "UIButton+RefreshLocation.h"
#import "WYPCustomLeftListHeader.h"
#import "WYPCustomLeftListAllBtnView.h"

@interface WYPCustomSelectLeftListView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property(nonatomic,  strong) UIButton                            *allBusinessBtn;
@property (nonatomic, strong) NSMutableArray                      *isExpandArray;
@property (nonatomic, assign) BOOL                                isSelectAllBtn;//是否点击了全业务线按钮
@property (nonatomic, strong) WYPCustomLeftListAllBtnView         *allBtnBgView;

@property (nonatomic, strong) WYPCustomSelectLeftListViewCell *cell;

@end
@implementation WYPCustomSelectLeftListView

#pragma mark -- lazy

-(WYPCustomLeftListAllBtnView *)allBtnBgView{
    if (!_allBtnBgView) {
        _allBtnBgView = [WYPCustomLeftListAllBtnView WYPCustomLeftListAllBtnView];
        [_allBtnBgView.topBtn addTarget:self action:@selector(allBusinessBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

    }
    
    return _allBtnBgView;
}
-(NSMutableArray *)isExpandArray{
    if (!_isExpandArray) {
        _isExpandArray = [[NSMutableArray alloc]init];
    }
    return _isExpandArray;
}

+(instancetype)WYPCustomSelectLeftListView{
  return [[[NSBundle mainBundle] loadNibNamed:@"WYPCustomSelectLeftListView" owner:nil options:nil]lastObject];
}

-(void)awakeFromNib{
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, kIPHONEXBOTTOM, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


//全部业务线按钮点击事件
-(void)allBusinessBtnClicked:(UIButton *)sender{
    
    //默认选中第一行,并执行点击事件
    self.isSelectAllBtn = YES;
    self.isRow = 0;
    self.inSection = self.dataArray.count > 1 ?  self.dataArray.count-1 : 0 ;

    
    if (self.isChangeLibrary)
    {
        if (self.allBtnBlock) {
            self.allBtnBlock(@"");
        }
    }
    else
    {
        [self settingTypeAllBusinessBtnISselect:YES];
        
        if (self.allBtnBlock) {
            self.allBtnBlock(@"");
        }
        
        [self.tableView reloadData];
    }
}
//设置全业务线按钮样式
-(void)settingTypeAllBusinessBtnISselect:(BOOL)isSelect{
    
    if (isSelect) {
        self.allBtnBgView.backgroundColor = [UIColor whiteColor];
        [self.allBtnBgView.allBtn setTitleColor:MainOrangeColor forState:UIControlStateNormal];
        self.allBtnBgView.allBtn.userInteractionEnabled = NO;

    }else{
        self.allBtnBgView.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
        [self.allBtnBgView.allBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        self.allBtnBgView.allBtn.userInteractionEnabled = YES;
    }
    
    self.allBtnBgView.leftLine.hidden = !isSelect;

}
#pragma mark - tableView代理和数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.isExpandArray[section] isEqualToString:@"0"]) {
        return 0;
        
    }else{//merchantArr
        
        if (section < self.dataArray.count) {
            WYPElectionBusinessModel *businessModel = self.dataArray[section];
            return businessModel.partnerProducts.count;
        }

        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WYPCustomSelectLeftListViewCell *cell = [WYPCustomSelectLeftListViewCell WYPCustomSelectLeftListViewCellWithTableView:tableView indexPath:indexPath];
    
    if (indexPath.section < self.dataArray.count) {
        WYPElectionBusinessModel *businessModel = self.dataArray[indexPath.section];
        if (indexPath.row < businessModel.partnerProducts.count) {
            WYPDatamodel *model = businessModel.partnerProducts[indexPath.row];
            
            cell.lineNameLabel.attributedText = [Tools attributedTextArray:@[model.productName] textColors:@[[UIColor colorWithHexString:@"#666666"]] textfonts:@[Font(11)] lineSpacing:1];
        }
    }

    if (indexPath.row == 0) {
        cell.linee.hidden = YES;
    }else{
        cell.linee.hidden = NO;
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44.f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44.f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    WYPCustomLeftListHeader *header = [WYPCustomLeftListHeader WYPCustomLeftListHeader];
    CGFloat width = 92;
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 32)];
    headerView.frame = header.bounds;
    [headerView addSubview:header];
    
    if (self.isExpandArray.count) {
        if ([self.isExpandArray[section] isEqualToString:@"0"]) {
            //未展开
            header.iconImgView.image = [UIImage imageNamed:@"btn_nav_down"];
            header.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
            header.dateLabel.textColor = [UIColor colorWithHexString:@"#666666"];
            header.dateLabel.font = Font(14);

        }else{
            //展开
            header.iconImgView.image = [UIImage imageNamed:@"btn_nav_up"];
            header.backgroundColor = [UIColor whiteColor];
            header.dateLabel.textColor = [UIColor blackColor];
            header.dateLabel.font = FontAndName(Semibold, 14);
        }

        [self addGeatureWithView:headerView section:section];
    }

    
    if (section < self.dataArray.count) {
        WYPElectionBusinessModel *businessModel = self.dataArray[section];
        
        header.dateLabel.text = businessModel.ruleName;
        return headerView;
    }

    return nil;
}
//设置手势事件
-(void)addGeatureWithView:(UIView *)view section:(NSInteger)section{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    tap.delegate = self;
    [view addGestureRecognizer:tap];
    view.tag = section;
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if ([self.isExpandArray[tap.view.tag] isEqualToString:@"0"]) {
        //        将所有关闭,展开当前
        [self.isExpandArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.isExpandArray replaceObjectAtIndex:idx withObject:@"0"];
        }];
        
        //关闭 => 展开
        [self.isExpandArray replaceObjectAtIndex:tap.view.tag withObject:@"1"];
        if (tap.view.tag < self.dataArray.count) {
            
            //动画
            CATransition * animation = [CATransition animation];
            animation.type = kCATransitionReveal;  //动画切换风格(揭开效果)
            animation.subtype = kCATransitionFromBottom; //动画切换方向
            animation.duration = 0.2f;
            [self.tableView.layer addAnimation:animation forKey:nil];
            [self.tableView reloadData];
        }
        
    }else{
        //展开 => 关闭
        if (tap.view.tag < self.isExpandArray.count) {
            [self.isExpandArray replaceObjectAtIndex:tap.view.tag withObject:@"0"];
            NSIndexSet *set = [NSIndexSet indexSetWithIndex:tap.view.tag];
            [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    
    if (!self.isSelectAllBtn || self.isHaveAllBusinessDefaultSellect) {//如果点击了全业务线按钮,再次展开列表不默认选中,如若默认选中,需要走默认选中方法
        [self settingSelectRow];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.isSelectAllBtn = NO;
    //    设置滚动
    [self settingCenterScrollWithTableView:tableView indexPath:indexPath];
    
    //默认选中第一行,并执行点击事件
    if (self.isNoSureChoose == NO) {
        
        self.inSection = indexPath.section;
        self.isRow = indexPath.row;
        self.indexPath = indexPath;
        
    }else{
        
        [self.tableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        if (self.indexPath != indexPath) {
            [tableView deselectRowAtIndexPath:indexPath animated:NO];
        }
    }
    
    if (indexPath.section < self.dataArray.count) {
        
        WYPElectionBusinessModel *businessModel = self.dataArray[indexPath.section];
        [self settingTypeAllBusinessBtnISselect:NO];
        if (indexPath.row < businessModel.partnerProducts.count) {
            WYPDatamodel *model = businessModel.partnerProducts[indexPath.row];
            if (self.selectBlock) {
                self.selectBlock(model,indexPath);
            }
        }
    }
}

-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;

    [self.isExpandArray removeAllObjects];
    [_dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (self.isHaveAllBusinessDefaultSellect) {//有默认选择
            
            if (idx == self.inSection) {
                
                [self.isExpandArray addObject:@"1"];//0:没展开 1:展开
            }else{
                [self.isExpandArray addObject:@"0"];//0:没展开 1:展开
            }

        }else{
            
            if (idx == self.dataArray.count-1) {
                [self.isExpandArray addObject:@"1"];//0:没展开 1:展开
            }else{
                [self.isExpandArray addObject:@"0"];//0:没展开 1:展开
            }
            
        }
    }];
  
    [self.tableView reloadData];
    
    if (!self.isHaveAllBusinessBtn) {//没有全业务线用默认选中
        [self settingSelectRow];
    }else{
        if (self.isHaveAllBusinessDefaultSellect) {//全业务线有默认选择值
            [self settingSelectRow];
        }
    }
    self.isSelectAllBtn = self.isHaveAllBusinessBtn;//默认全业务线按钮是选中的
    
    self.indexPath = [NSIndexPath indexPathForRow:self.isRow inSection:self.inSection];
    
    
}


- (void)layoutSubviews{
    [super layoutSubviews];
    self.width = 92;
    [self setUI];
}

//设置选中行
-(void)settingSelectRow{

    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.isRow inSection:self.inSection] animated:YES scrollPosition:UITableViewScrollPositionNone];
}

-(void)setUI{
    
    if (self.isHaveAllBusinessBtn) {//有全业务线
        //  tableViewHeadView
        UIView *tableViewHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 92, 45)];
        self.allBtnBgView.allBtn.userInteractionEnabled = NO;
        [tableViewHeadView addSubview:self.allBtnBgView];
        
        //    间隔线
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 92, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#ECECEC"];
        [tableViewHeadView addSubview:line];
        
        self.tableView.tableHeaderView = tableViewHeadView;
    }else{//没有全业务线
        
        self.tableView.tableHeaderView = [UIView new];
    }
}

//设置滚动视图
-(void)settingCenterScrollWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    
    if ([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[WYPCustomSelectLeftListViewCell class]]) {
        
        WYPCustomSelectLeftListViewCell *cell = (WYPCustomSelectLeftListViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        
        if (tableView.contentSize.height >= self.height) {
            
            CGRect frame=[cell convertRect: cell.bounds toView:tableView];
            CGFloat selectY = frame.origin.y + frame.size.height * 0.5;
            
            if (selectY >= self.height * 0.5) {
                
                if (tableView.contentSize.height - selectY > self.height * 0.5){
                    [tableView setContentOffset:CGPointMake(0, selectY - self.height * 0.5) animated:YES];
                }else{
                    [tableView setContentOffset:CGPointMake(0,tableView.contentSize.height - self.height) animated:YES];
                }
                
            }else{
                [tableView setContentOffset:CGPointZero animated:YES];

            }
        }
    }
}

@end

//
//  Created by 王彦平 on 2021/2/20.
//  Copyright © 2021 JH. All rights reserved.
//
#import "TestLestListViewVC.h"
#import "WYPCustomSelectLeftListView.h"
#import "WYPElectionBusinessModel.h"

@interface TestLestListViewVC ()

@property (weak, nonatomic) IBOutlet UIView                *leftBgView;
@property (nonatomic, strong) WYPCustomSelectLeftListView  *leftView;
@property (nonatomic, strong) NSIndexPath                  *indexPath;
@property (nonatomic, strong) NSIndexPath                  *indexPathTemp;
@property (nonatomic,assign) BOOL                          isNoSureChoose;//是否已经确认选中(NO为选中)

@property (weak, nonatomic) IBOutlet UIButton              *aleartBtn;


@end

@implementation TestLestListViewVC

//左视图
-(WYPCustomSelectLeftListView *)leftView{
    if (!_leftView) {
        _leftView = [WYPCustomSelectLeftListView WYPCustomSelectLeftListView];
        _leftView.isHaveAllBusinessBtn = NO;
        _leftView.isHaveAllBusinessDefaultSellect = YES;
        WS(weakSelf);
        _leftView.selectBlock = ^(WYPDatamodel * _Nonnull model,NSIndexPath * _Nonnull indexPath){//cell点击事件
            
            [weakSelf settingInterfaceWithModel:model indexPath:indexPath];
            
        };
    }
    return _leftView;
}
-(instancetype)init{
    if (self = [super initWithNibName:@"TestLestListViewVC" bundle:[NSBundle mainBundle]]) {
        return self;
    }
    return nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //    左视图
    self.leftView.frame = self.leftBgView.bounds;
    [self.leftBgView addSubview:self.leftView];
    
    [self  requestSCrollData];

}

#pragma mark - 网络请求
//业务线请求
-(void)requestSCrollData{
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    param[@"token"] = [WYPSaveUtils share].token;
    [LRPNetworkHelper POSTAES:kProduct_mine_rule parameters:param success:^(id responseObject) {
        NSString *infoStr = responseObject[@"rtnInfo"];
        [WYPSVProgressHUD dismissIndicator];
        if (SUCCESS) {
            [self.dataArray removeAllObjects];
            
            NSArray *dictArr = responseObject[@"data"];
            
            [dictArr enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                WYPElectionBusinessModel *model = [[WYPElectionBusinessModel alloc]initWithDict:obj];
                [self.dataArray addObject:model];
            }];
            
            if (self.dataArray.count) {
                //默认数组最后一个的选中第一行,
                self.leftView.inSection = self.dataArray.count - 1;
                self.leftView.isRow = 0;
                self.indexPath = [NSIndexPath indexPathForRow:0 inSection:self.dataArray.count - 1];
            }
            self.leftView.dataArray = self.dataArray;
            
        }else{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:infoStr];
            });
        }
        
    } failure:^(NSError *error) {
        [WYPSVProgressHUD dismissIndicator];
    }];
}

- (IBAction)aleartBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        self.isNoSureChoose = YES;
    }else{
        self.isNoSureChoose = NO;
    }
    
    self.leftView.isNoSureChoose = self.isNoSureChoose;
}

//数据处理部分
-(void)settingInterfaceWithModel:(WYPDatamodel *)model indexPath:(NSIndexPath *)indexPath{
    
    
       if (self.isNoSureChoose == NO) {
           self.indexPath = indexPath;
       }else{
           self.indexPathTemp = indexPath;
           
           [Tools showAlertWithTitle:@"" message:@"是否去做一件事情" items:@[@"取消",@"确定"] atController:self action:^(NSUInteger itemIndex) {
               
               if (itemIndex) {//确定===================
                   
                   self.isNoSureChoose = NO;
                   self.indexPath = self.indexPathTemp;
                   self.leftView.indexPath = self.indexPath;
                   self.leftView.isNoSureChoose =  self.isNoSureChoose;

                   
                   if (self.dataArray.count) {
                       //数组选中行,
                       self.leftView.inSection = self.indexPath.section;
                       self.leftView.isRow = self.indexPath.row;
                   }

                   [self.leftView.tableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                   
               }else{//取消===============================
                   
                   self.isNoSureChoose = YES;
                   self.leftView.isNoSureChoose =  self.isNoSureChoose;
                   self.leftView.indexPath = self.indexPath;
                   
                   if (self.dataArray.count) {
                       //数组选中行,
                       self.leftView.inSection = self.indexPath.section;
                       self.leftView.isRow = self.indexPath.row;
                   }

                   [self.leftView.tableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

               }
               self.aleartBtn.selected = self.isNoSureChoose;

           }];

       }
       

}
@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值