From a17a0062e1284aab4da4cc40bc7ccf3c458f0770 Mon Sep 17 00:00:00 2001 From: maxmoo Date: Thu, 24 Mar 2016 13:37:09 +0800 Subject: [PATCH] add new pro --- CCActionSheet/CCActionSheet.h | 8 +++--- CCActionSheet/CCActionSheet.m | 48 ++++++++++++++++++++++++++++++----- Demo/ViewController.m | 17 +++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/CCActionSheet/CCActionSheet.h b/CCActionSheet/CCActionSheet.h index 7d6e42b..aff7f43 100644 --- a/CCActionSheet/CCActionSheet.h +++ b/CCActionSheet/CCActionSheet.h @@ -50,7 +50,6 @@ typedef enum : NSUInteger { @property (strong,nonatomic) NSString *cancelText; @property (weak, nonatomic) id delegate; - //最多可显示maxcount个cell的高度 @property (assign, nonatomic) CGFloat maxCount; //cell的样式,有两种:一种居中显示,另一种左对齐显示并带有icon @@ -66,7 +65,7 @@ typedef enum : NSUInteger { * @param cancelButtonTitle 取消按钮标题 * @param otherButtonTitles 其他按钮标题 * - * @return PQActionSheet + * @return ActionSheet */ - (instancetype)initWithTitle:(NSString *)title delegate:(id)delegate @@ -83,7 +82,7 @@ typedef enum : NSUInteger { * @param cancelButtonTitle 取消按钮标题 * @param otherButtonTitles 其他按钮标题 * - * @return PQActionSheet + * @return ActionSheet */ - (instancetype)initWithTitle:(NSString *)title clickedAtIndex:(ClickedIndexBlock)block @@ -91,6 +90,9 @@ typedef enum : NSUInteger { otherButtonTitles:(NSString *)otherButtonTitles, ...; +//直接添加自定义视图 +- (instancetype)initWithCustomView:(UIView *)customView; + /** * @brief 显示ActionSheet diff --git a/CCActionSheet/CCActionSheet.m b/CCActionSheet/CCActionSheet.m index c99f339..c4709ce 100644 --- a/CCActionSheet/CCActionSheet.m +++ b/CCActionSheet/CCActionSheet.m @@ -23,6 +23,8 @@ @interface CCActionSheet () @property (assign,nonatomic) CGFloat tableViewHeight; @property (assign,nonatomic) NSInteger buttonCount; +@property (strong,nonatomic) UIView *customView; + @end @implementation CCActionSheet @@ -94,6 +96,16 @@ - (instancetype)initWithTitle:(NSString *)title return self; } +//添加自定义视图 +- (instancetype)initWithCustomView:(UIView *)customView{ + self = [super init]; + if (self) { + self.customView = customView; + [self installSubViews]; + } + return self; +} + - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; @@ -110,6 +122,7 @@ - (void)show [[UIApplication sharedApplication].keyWindow addSubview:self]; self.tableView.frame = CGRectMake(0.0f,self.bounds.size.height, self.bounds.size.width, self.tableViewHeight); + NSLog(@"%@",self.tableView); __weak typeof(self) weakSelf = self; if([_delegate respondsToSelector:@selector(willPresentActionSheet:)]) { @@ -266,6 +279,11 @@ - (UIView *)selectedView{ */ -(CGFloat)tableViewHeight { + if (self.customView) { + NSLog(@"%f",self.customView.bounds.size.height); + return self.customView.bounds.size.height; + } + CGFloat tableHeight = 0.0f; if (_maxCount) { @@ -312,12 +330,19 @@ -(NSInteger)buttonCount { #pragma mark - UITableViewDelegate -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - return ACTION_SHEET_BTN_HEIGHT; + if (self.customView) { + return self.customView.bounds.size.height; + } + return ACTION_SHEET_BTN_HEIGHT; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { + if (self.customView) { + return 0.0f; + } + if(section == 0 && self.titleText) { return ACTION_SHEET_BTN_HEIGHT; @@ -429,14 +454,21 @@ -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS cell.sheetStyle = (CCActionSheetCellStyle)self.style; } cell.boundsTableView = self.tableView; - // 加上分割线 - UIImageView *sepLine = [[UIImageView alloc]initWithImage:[self imageWithUIColor:[UIColor grayColor]]]; - sepLine.frame = CGRectMake(0, ACTION_SHEET_BTN_HEIGHT - 0.3f, [UIScreen mainScreen].bounds.size.width, 0.3f); - [sepLine setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - [cell addSubview:sepLine]; + + if (!self.customView) { + // 加上分割线 + UIImageView *sepLine = [[UIImageView alloc]initWithImage:[self imageWithUIColor:[UIColor grayColor]]]; + sepLine.frame = CGRectMake(0, ACTION_SHEET_BTN_HEIGHT - 0.3f, [UIScreen mainScreen].bounds.size.width, 0.3f); + [sepLine setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; + [cell addSubview:sepLine]; + } } if(indexPath.section == 0){ + if (indexPath.row == 0 && self.customView) { + self.customView.center = CGPointMake(self.tableView.bounds.size.width/2,self.tableView.bounds.size.height/2); + [cell.contentView addSubview:self.customView]; + } if (_iconImageNameArray.count > indexPath.row) { cell.iconImageName = _iconImageNameArray[indexPath.row]; } @@ -469,6 +501,10 @@ -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + if (self.customView) { + return 1; + } + if(section == 0) { return self.otherButtons.count; diff --git a/Demo/ViewController.m b/Demo/ViewController.m index 6fc54da..9907434 100644 --- a/Demo/ViewController.m +++ b/Demo/ViewController.m @@ -42,9 +42,26 @@ - (IBAction)showCCActionSheet:(UIButton *)sender { sheet.iconImageNameArray = @[@"icon_connected",@"icon_connected",@"icon_connected",@"icon_connected",@"icon_connected",@"icon_connected",@"icon_connected",@"icon_connected",@"icon_connected"]; +// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; +// view.backgroundColor = [UIColor redColor]; +// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; +// button.frame = CGRectMake(0, 0, 50, 30); +// button.backgroundColor = [UIColor redColor]; +// [button setTitle:@"click" forState:UIControlStateNormal]; +// [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; +// [view addSubview:button]; + + + +// CCActionSheet *sheet = [[CCActionSheet alloc] initWithCustomView:view]; + [sheet show]; } +- (void)buttonClicked{ + NSLog(@"11111"); +} + - (void)cc_actionSheetDidSelectedIndex:(NSInteger)index{ NSLog(@"selected index:%ld",(long)index); }