自定义的alert view1( 用tableview实现)

.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ENCustomBottomTableviewCell : UITableViewCell
@property (nonatomic, strong) UILabel *lblContent;
+ (ENCustomBottomTableviewCell *)getCellWithTableView:(UITableView *)tableView;
/**
 *  arrData      初始数据源
 *  strSelcted   初始选中的数据源
 *  indexPath    行
 *  strTitle   区分类型
 
 **/
- (void)configCellWithArr:(NSMutableArray *)arrData  selectedStr:(NSMutableArray*)arrSelcted  indexPath:(NSIndexPath *)indexPath  strTitle:(NSString *)strTitle;
@end


typedef void(^btnFinishClickBlock)(NSString *strText,NSInteger selectedIndex,NSDictionary *dicItem);
@interface ENCustomChooseTableView : UIView
@property (copy, nonatomic) btnFinishClickBlock finishBlock;

/**
 *
 *  弹出底部选择试图
 *
 *  @param frame          显示选择部分的大背景frame
 *  @param title          标题
 *  @param contentArray   数据源
 *  @param selectArray    选中的数据
 *  @param chooseIndex    1 医院 2医院地址
 *  @param leftTitle      取消
 *  @param rigthTitle       标题文字

 
 *  展示
 */
- (instancetype)initWithContentFrame:(CGRect)frame
        Title:(NSString *)title
       contentArray:(NSMutableArray *)contentArray
          selectArr:(NSMutableArray *)selectArray
        chooseIndex:(NSInteger)chooseIndex
    leftButtonTitle:(NSString *)leftTitle
   rightButtonTitle:(NSString *)rigthTitle;
- (void)disappear;
- (void)show;
@end

NS_ASSUME_NONNULL_END

.m文件

#import "ENCustomChooseTableView.h"
#import "MBProgressHUD.h"
#import "NSString+Calculate.h"
@interface ENCustomBottomTableviewCell ()
@property (nonatomic, strong) UIView  *vSepLine;
@end
@implementation ENCustomBottomTableviewCell
+ (ENCustomBottomTableviewCell *)getCellWithTableView:(UITableView *)tableView{
    
    static NSString * ID = @"ENCustomBottomTableviewCell";
    ENCustomBottomTableviewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if ( !cell ) {
        cell = [[ENCustomBottomTableviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell updateConstraintsIfNeeded];
        cell.backgroundColor = Color_White;

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
- (void)updateConstraints{
    [super updateConstraints];
    [self.lblContent mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15*WIDTHRADIUS);
        make.right.equalTo(self.contentView.mas_right).offset(-15*WIDTHRADIUS);
        make.top.equalTo(self.contentView.mas_top).offset(5*WIDTHRADIUS);
        make.height.mas_greaterThanOrEqualTo(40*WIDTHRADIUS);
    }];
    [self.vSepLine  mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblContent.mas_bottom).offset(5*WIDTHRADIUS);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-1);
        make.left.equalTo(self.contentView.mas_left).offset(15*WIDTHRADIUS);
        make.right.equalTo(self.contentView.mas_right).offset(-15*WIDTHRADIUS);
    }];
}
- (UILabel *)lblContent{
    if (!_lblContent) {
        _lblContent = [XWTool createLabelWithFrame:CGRectZero text:@"" font:REGULAR_FONT(13) textColor:Color_SubTitle textAlignment:NSTextAlignmentCenter SuperView:self.contentView];
        _lblContent.numberOfLines = 0;
    }
    return _lblContent;
}

- (UIView *)vSepLine{
    if (!_vSepLine) {
        _vSepLine = [XWTool createViewWithFrame:CGRectZero backgroundColor:Color_Separateline superView:self.contentView];
    }
    return _vSepLine;
}



/**
 *  arrData      初始数据源
 *  strSelcted   初始选中的数据源
 *  indexPath    行
 *  strTitle   区分类型
 
 **/


- (void)configCellWithArr:(NSMutableArray *)arrData  selectedStr:(NSMutableArray*)arrSelcted  indexPath:(NSIndexPath *)indexPath  strTitle:(NSString *)strTitle{
    if([strTitle isEqual:@"性别"]){
        self.lblContent.text = arrData[indexPath.row][@"name"];
        NSInteger indexRow = -1 ;
        for (int i = 0;i<arrData.count ; i ++) {
            if (arrSelcted.count > 0 &&[arrSelcted[0] isEqualToString:arrData[i][@"name"]]) {
                indexRow = i;
                continue;
            }
        }
        if (indexPath.row == indexRow ) {
            self.lblContent.textColor = Color_Theme;
        }else{
            self.lblContent.textColor = Color_Title;
        }
    }
    
    else if([strTitle  isEqualToString:@"矫治器类型"]){//矫治器类型
        
        self.lblContent.text = arrData[indexPath.row][@"Name"];
        NSInteger indexRow = -1 ;
        for (int i = 0;i<arrData.count ; i ++) {
            if (arrSelcted.count > 0 &&[arrSelcted[0] isEqualToString:arrData[i][@"Name"]]) {
                indexRow = i;
                continue;
            }
        }
        if (indexPath.row == indexRow ) {
            self.lblContent.textColor = Color_Theme;

        }else{
            self.lblContent.textColor = Color_Title;

        }

    }else if ([strTitle  isEqualToString:@"医疗机构"]) {//医院名称
        self.lblContent.text = arrData[indexPath.row][@"HospitalName"];
      NSInteger indexRow = -1 ;
        for (int i = 0; i < arrData.count; i ++) {
            if (arrSelcted.count > 0 &&[arrSelcted[0] isEqualToString:arrData[i][@"HospitalName"]]) {
                indexRow = i;
                
                continue;
            }
        }
        if (indexPath.row == indexRow ) {
            self.lblContent.textColor = Color_Theme;

        }else{
            self.lblContent.textColor = Color_Title;

        }
    }else if ([strTitle  isEqualToString:@"收货地址"]){//收货地址
        self.lblContent.text = arrData[indexPath.row][@"Address"];
        NSInteger indexRow = -1 ;
        for (int i = 0; i < arrData.count; i ++) {
            if (arrSelcted.count > 0 &&[arrSelcted[0] isEqualToString:arrData[i][@"Address"]]) {
                indexRow = i;
                continue;
            }
        }
        if (indexPath.row == indexRow ) {
            self.lblContent.textColor = Color_Theme;

        }else{
            self.lblContent.textColor = Color_Title;

        }
    }else if ([strTitle  isEqualToString:@"模型组"]){//收货地址
        self.lblContent.text = arrData[indexPath.row][@"Name"];
        NSInteger indexRow = -1 ;
        for (int i = 0; i < arrData.count; i ++) {
            if (arrSelcted.count > 0 &&[arrSelcted[0] isEqualToString:arrData[i][@"Name"]]) {
                indexRow = i;
                continue;
            }
        }
        if (indexPath.row == indexRow ) {
            self.lblContent.textColor = Color_Theme;
        }else{
            self.lblContent.textColor = Color_Title;

        }
       
    }
//
//    CGSize newSize = [_lblContent.text sizeWithFontCustom:REGULAR_FONT(14) constrainedToSize:CGSizeMake(ViewWidth(self.contentView), MAXFLOAT)];
//    if (newSize.height  > 44) {
//        [self.lblContent mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(newSize.height);
//        }];
//    }else {
//        [self.lblContent mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(44);
//        }];
//    }
//    [self layoutIfNeeded];
    
}
- (UITableView *)myTableView
{
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}
- (void)layoutSubviews{
    
}

#pragma mark - getters



#pragma mark - attributeStr


- (void)setFrame:(CGRect)frame{
    
    [super setFrame:frame];
}




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

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super  setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
@end



@interface ENCustomChooseTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic)UIView             *vCoverBg;
@property (strong, nonatomic)UIView             *vContentBg;
@property (strong, nonatomic)NSArray            *arrChooseList;
@property (strong, nonatomic)UIButton           *btnFinish;
@property (strong, nonatomic)UILabel            *lblTitle;
@property (strong, nonatomic)UIView             *vHeadBg;
@property (strong, nonatomic)UIPickerView       *pickChoose;
@property (nonatomic)        CGRect             frameContent;
@property (assign, nonatomic)NSInteger          selectedIndex;
@property (copy, nonatomic)  NSString           *strPlatform;
@property (strong, nonatomic)UIButton           *btnCancel;
@property (strong, nonatomic)UIView             *vSepCenter;
@property (strong, nonatomic)UIView             *vSepline;
@property (assign, nonatomic)NSInteger          sectionIndex;
@property (strong, nonatomic)UITableView         *tbvMain;
@property (strong, nonatomic)NSMutableArray         *arrContent;
@property (strong, nonatomic)NSMutableArray         *arrSelect;
@property (nonatomic,assign) NSInteger chooseIndex;
@property (nonatomic,assign) NSInteger userSelectedIndex;//选中的row
@property (nonatomic,strong) NSDictionary  *dicSelected;//选中的dic
@property (nonatomic,copy) NSString  *strText;//文字
@property (nonatomic,strong) UILabel  *lblChooseText;//选中状态
@property (copy, nonatomic)  NSString           *strChooseTitle;//哪一个分类
@property ( nonatomic,strong)  NSIndexPath          *lastIndexPath;//哪一个分类
@end
@implementation ENCustomChooseTableView

/**
 *
 *  弹出底部选择试图
 *
 *  @param frame          显示选择部分的大背景frame
 *  @param title          标题
 *  @param contentArray   数据源
 *  @param selectArray    选中的数据
 *  @param chooseIndex    1 医院 2医院地址
 *  @param leftTitle      取消
 *  @param rigthTitle       标题文字
 
 
 *  展示
 */
- (instancetype)initWithContentFrame:(CGRect)frame
                               Title:(NSString *)title
                        contentArray:(NSMutableArray *)contentArray
                           selectArr:(NSMutableArray *)selectArray
                         chooseIndex:(NSInteger)chooseIndex
                     leftButtonTitle:(NSString *)leftTitle
                    rightButtonTitle:(NSString *)rigthTitle{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createSubviewWithContentFrame:frame Title:title contentArray:contentArray selectArr:selectArray chooseIndex:chooseIndex leftButtonTitle:leftTitle rightButtonTitle:rigthTitle];
    }
    return self;
}


- (void)createSubviewWithContentFrame:(CGRect)frame
                               Title:(NSString *)title
                        contentArray:(NSMutableArray *)contentArray
                           selectArr:(NSMutableArray *)selectArray
                         chooseIndex:(NSInteger)chooseIndex
                     leftButtonTitle:(NSString *)leftTitle
                     rightButtonTitle:(NSString *)rigthTitle{
    _frameContent = frame;
    _arrContent = contentArray;
    _arrSelect   = selectArray;
    _chooseIndex = chooseIndex  ;
    _strChooseTitle = title;

    //获取之前选中的indexPath
  
    
    for (int i = 0; i < _arrContent.count; i++) {
        if ([_arrSelect containsObject:_arrContent[i]]) {
            
            self.lastIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        }
        
    }
    
    
    
    UIWindow *win = [UIApplication  sharedApplication].keyWindow ;
    [win addSubview:self];
    self.frame = win.frame;
    
    //蒙版
    
    
    if (!_vCoverBg) {
        _vCoverBg =   [XWTool createViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) backgroundColor:[UIColor colorWithWhite:0 alpha:0.3] superView:self];
    }
    if (!_vContentBg) {
        _vContentBg =  [XWTool createViewWithFrame:frame backgroundColor:Color_White superView:self];
    }
    
    if (!_vHeadBg) {
        _vHeadBg =  [XWTool createViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) backgroundColor:[UIColor whiteColor] superView:_vContentBg];
    }
    
    if (!_btnCancel) {
        _btnCancel = [XWTool createButtonWithFrame:CGRectMake(0, 0, 100, 44) title:NSLocalizedString(@"add_Prescription_tipShow10", nil) setImage:@"" setBgImg:@"" font:REGULAR_FONT(14) titleColor:Color_Alert_Gray backgroundColor:[UIColor clearColor] SuperView:_vHeadBg];
        
    }
    [_btnCancel addTarget:self action:@selector(btnCancelClick) forControlEvents:UIControlEventTouchUpInside];
    
    if (!_btnFinish) {
        _btnFinish = [XWTool createButtonWithFrame:CGRectMake(SCREEN_WIDTH - 100, 0, 100, 44) title:NSLocalizedString(@"add_Prescription_tipShow13", nil) setImage:@"" setBgImg:@"" font:REGULAR_FONT(14) titleColor:Color_Theme    backgroundColor:[UIColor clearColor] SuperView:_vHeadBg];
    }
    
    [_btnFinish addTarget:self action:@selector(btnFinishClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_btnCancel setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    [_btnFinish setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    if (!_vSepline) {
        _vSepline = [XWTool createViewWithFrame:CGRectMake(0, 44-1,SCREEN_WIDTH, 1) backgroundColor:Color_Separateline superView:_vContentBg];
        
    }
    
    if (!_tbvMain) {
        _tbvMain = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_vHeadBg.frame), SCREEN_WIDTH, ViewHeight(_vContentBg) - CGRectGetMaxY(_vHeadBg.frame)) style:UITableViewStylePlain];
        _tbvMain.backgroundColor = Color_White;
        _tbvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tbvMain.delegate = self;
        _tbvMain.dataSource = self;
        [_vContentBg addSubview:_tbvMain];
        
        
    }
    
    
    [_tbvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(44, 0, 0, 0));
        
    }];
        //    if (!_pickChoose) {
    //        _pickChoose = [[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_vHeadBg.frame), SCREEN_WIDTH, ViewHeight(_vContentBg) - CGRectGetMaxY(_vHeadBg.frame))];
    //        _pickChoose.delegate = self;
    //        _pickChoose.dataSource = self;
    //        _pickChoose.showsSelectionIndicator = YES;
    //        [_vContentBg addSubview:_pickChoose];
    //    }
    //
    //
        //添加手势
    UITapGestureRecognizer  * pin = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disappear)];
    [_vContentBg  addGestureRecognizer:pin];
    [_vCoverBg addGestureRecognizer:pin];
}




//展示
- (void)show{
    DefinitionWeak(self);

    [UIView animateWithDuration:0.3 animations:^{
        weak_self.vCoverBg.hidden = NO;
        weak_self.vContentBg.frame = CGRectMake(0, SCREEN_HEIGHT - weak_self.frameContent.size.height, SCREEN_WIDTH,weak_self.frameContent.size.height);
    }];
}

//消失
- (void)disappear{
    DefinitionWeak(self);

    [UIView animateWithDuration:0.3 animations:^{
        weak_self.vCoverBg.hidden = YES;
        weak_self.vContentBg.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, weak_self.frameContent.size.height);
        [weak_self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.3];
    }];
}


//完成按钮点击
- (void)btnFinishClick{
//    if (_strPlatform.length ==0) {
//        _strPlatform = _arrChooseList[0][@"name"];
//
//        _selectedIndex = [_arrChooseList[0][@"value"] integerValue];
//    }
//    if(_finishBlock){
//        _finishBlock(_strPlatform,_selectedIndex);
//    }
    
    //当选中某一个选项的时候 将对应的值传过去
    
    if (_strText.length > 0) {
        if (_finishBlock) {
            _finishBlock(_strText,_selectedIndex,_dicSelected );
        }
    }
    
    [self disappear];
}

- (void)btnCancelClick{
    [self disappear];
}

#pragma mark delegate

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrContent.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ENCustomBottomTableviewCell *cell = [ENCustomBottomTableviewCell getCellWithTableView:tableView];
    if (indexPath.row < self.arrContent.count ) {
        [cell configCellWithArr:_arrContent selectedStr:_arrSelect indexPath:indexPath strTitle:self.strChooseTitle];
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (_arrSelect.count > 0) {
        [_arrSelect removeAllObjects];
    }
    if ([self.strChooseTitle isEqualToString:@"xx"]) {
                [_arrSelect addObject:self.arrContent[indexPath.row][@"Name"]];
              _dicSelected = self.arrContent[indexPath.row];

                _strText = self.arrContent[indexPath.row][@"Name"];
                _selectedIndex  = indexPath.row;
    }
    [tableView   reloadData];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
    
    
- (void)setDentyType:(NSInteger)dentyType{
//    _dentyType = dentyType;
    
}

    

- (void)showMessage:(NSString *)msg {
    if (!msg || [msg isEqualToString:@""]) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.userInteractionEnabled = NO;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = FontSystem(18);
    hud.margin = 15;
    hud.backgroundView.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2.0];
}

@end

猜你喜欢

转载自blog.csdn.net/weixin_42050662/article/details/122734299