iOS-自定义sheetAlertView的封装

前言

  • 在我们开发过程中会遇到很多需要自定义的sheetAlertView的控件,下面是一个自定义的封装,下面是效果图
    在这里插入图片描述

准备工作

  • 安装pod - 自己可以找下教程这里就不详细解答
  • cd你所在工程,导入TYAlertController,这是github中的开源控件
    • pod 'TYAlertController'
    • pod 'MJExtension'
    • 导入后为下图
      在这里插入图片描述
  • 使用MVC的思想 网络数据 转自定义模型MC中赋值给V

直接上代码 - 一共三个类 EditSheetModel ,EditSheetView 和 EditSheetShow

EditSheetModel

  • EditSheetModel.h
#import <Foundation/Foundation.h>

@interface EditSheetModel : NSObject

@property(nonatomic, strong) NSString *dataTitle;

@end
  • EditSheetModel.m
#import "EditSheetModel.h"

@implementation EditSheetModel

@end

EditSheetView

  • EditSheetView.h
#import <UIKit/UIKit.h>

@class EditSheetModel;
@interface EditSheetView : UIView

@property(nonatomic, copy) void(^cancelBtnBlock)(void);
@property(nonatomic, copy) void(^clickBtnBlock)(NSIndexPath *index, NSString *string);

- (instancetype)initWithFrame:(CGRect)frame titleText:(NSString *)title data:(NSMutableArray<EditSheetModel *> *)dataArr;

@end
  • EditSheetView.m
#import "EditSheetView.h"
#import "EditSheetModel.h"

#define LYWidth(width) (1 * (width))//屏幕缩放因子,这里暂时用这个代替
#define SAFEAREA    (SCREEN_HEIGHT == 812.0 ? LYWidth(28) : 0)//iphoneX底部安全高度
#define SCREEN_HEIGHT  [[UIScreen mainScreen] bounds].size.height// 设备全屏高
#define SCREEN_WIDTH   [[UIScreen mainScreen] bounds].size.width// 设备全屏宽
static NSString *const kEditSheetViewCellID = @"kEditSheetViewCellID";
@interface EditSheetView () <UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray<EditSheetModel *> *listArr;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, strong) UIButton *cancelBtn;

@end

@implementation EditSheetView

- (instancetype)initWithFrame:(CGRect)frame titleText:(NSString *)title data:(NSMutableArray<EditSheetModel *> *)dataArr {
    if(self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        self.title = title;
        self.listArr = dataArr;
        self.frame = CGRectMake(0, SCREEN_HEIGHT - LYWidth(14 + 50) - self.listArr.count * LYWidth(50) - LYWidth(50) - SAFEAREA, SCREEN_WIDTH, LYWidth(14 + 50) + self.listArr.count * LYWidth(50) + LYWidth(50) + SAFEAREA);
        [self addSubview:self.tableView];
        [self addSubview:self.cancelBtn];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (NSMutableArray<EditSheetModel *> *)listArr {
    if(_listArr == nil) {
        _listArr = [NSMutableArray<EditSheetModel *> array];
    }
    return _listArr;
}

- (UIButton *)cancelBtn {
    if(_cancelBtn == nil) {
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(LYWidth(9.5), CGRectGetMaxY(self.tableView.frame) + LYWidth(8.5), SCREEN_WIDTH - LYWidth(19), LYWidth(50))];
        [_cancelBtn setTitleColor:LY_Color_Hex(#333333) forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.layer.masksToBounds = YES;
        _cancelBtn.layer.cornerRadius = LYWidth(13);
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:LYWidth(19)];
        _cancelBtn.backgroundColor = [UIColor whiteColor];
        [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

#pragma mark - tableView

- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(LYWidth(9.5), 0, SCREEN_WIDTH - LYWidth(19), LYWidth(50) + self.listArr.count * LYWidth(50)) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.layer.masksToBounds = YES;
        _tableView.layer.cornerRadius = LYWidth(13);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = LYWidth(50);
        _tableView.rowHeight = LYWidth(50);
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kEditSheetViewCellID];
    }
    return _tableView;
}

#pragma mark - tableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EditSheetModel *model = self.listArr[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kEditSheetViewCellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, LYWidth(1))];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [cell addSubview:lineView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - LYWidth(19), LYWidth(50))];
    label.textColor = LY_Color_Hex(#333333);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:LYWidth(19)];
    [cell addSubview:label];
    
    label.text = [NSString stringWithFormat:@"%@",model.dataTitle];
    
    return cell;
}

#pragma mark - tableView Header dataSource

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - LYWidth(19), LYWidth(50))];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]initWithFrame:view.bounds];
    label.text = [NSString stringWithFormat:@"%@",self.title];
    label.textColor = LY_Color_Hex(#333333);
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    return view;
}

#pragma mark - tableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(self.clickBtnBlock) {
        EditSheetModel *model = self.listArr[indexPath.row];
        self.clickBtnBlock(indexPath, model.dataTitle);
    }
}

- (void)cancelClick:(UIButton *)sender {
    if(self.cancelBtnBlock) {
        self.cancelBtnBlock();
    }
}

@end

例子的使用

  • ViewController.m - 直接在新创建的工程的ViewController上使用
#import "ViewController.h"
#import "TestTableView.h"

@interface ViewController ()

@property(nonatomic, strong) TestTableView *testView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.testView];
    [self.testView setJsonName:@"12345.json"];
}

- (TestTableView *)testView {
    if(_testView == nil) {
        _testView = [[TestTableView alloc]initWithFrame:self.view.bounds];
    }
    return _testView;
}

@end

EditSheetShow

  • EditSheetShow.h
@class EditSheetModel;
typedef void(^IndexClickBlock)(NSIndexPath *index, NSString *title);
typedef void(^CancelBlock)(void);

@interface EditSheetShow : TYAlertController
+ (void)showAlert:(UIViewController *)presentingViewController
        titleText:(NSString *)title
             data:(NSMutableArray<EditSheetModel *> *)dataArr
       indexClick:(IndexClickBlock)clickBlock;
@end
  • EditSheetShow.m
#import "EditSheetShow.h"
#import "EditSheetView.h"
#import "EditSheetModel.h"

@implementation EditSheetShow

+ (void)showAlert:(UIViewController *)presentingViewController
        titleText:(NSString *)title
             data:(NSMutableArray<EditSheetModel *> *)dataArr
       indexClick:(IndexClickBlock)clickBlock {
    __block EditSheetShow *alertController = nil;
    EditSheetView *alertView = [[EditSheetView alloc] initWithFrame:CGRectZero titleText:title data:dataArr];
    alertController = [EditSheetShow alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleActionSheet];
    alertController.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];//背景透明系数 0.0 - 1.0
    alertController.backgoundTapDismissEnable = YES;
    [alertView setCancelBtnBlock:^{
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertView setClickBtnBlock:^(NSIndexPath * _Nonnull index, NSString * _Nonnull string) {
        [alertController dismissViewControllerAnimated:YES completion:^{
            clickBlock ? clickBlock(index, string) : NULL;
        }];
    }];
    [presentingViewController presentViewController:alertController animated:YES completion:nil];
}

@end

使用

ViewController.m

#import "ViewController.h"

#import "EditSheetShow.h"
#import "EditSheetModel.h"

@interface ViewController ()

@property(nonatomic, strong) NSMutableArray<EditSheetModel *> *listArr;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [EditSheetShow showAlert:self
                   titleText:@"选择字母"
                        data:self.listArr
                  indexClick:^(NSIndexPath *index, NSString *title) {
                      NSLog(@"index %@ - title %@",index, title);
                  }];
}

- (NSMutableArray<EditSheetModel *> *)listArr {
    if(_listArr == nil) {
        NSArray *array = [NSArray arrayWithObjects:@{@"dataTitle":@"A"},@{@"dataTitle":@"B"},@{@"dataTitle":@"C"},nil];
        _listArr = [NSMutableArray<EditSheetModel *> array];
        _listArr = [EditSheetModel mj_objectArrayWithKeyValuesArray:array];
    }
    return _listArr;
}

@end

蓝湖Hex十六进制字符直接取色

  • 直接戳下面链接,有蓝湖Hex的直接去色宏的封装 ,如果上面的代码中,暂时不想用,可以直接将宏去掉换成 [UIColor whiteColor]即可
  • LY_Color_Hex(#333333)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值