热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

iOS下拉选择菜单简单封装

这篇文章主要为大家详细介绍了iOS下拉选择菜单封装代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了简单封装的iOS下拉选择菜单代码,供大家参考,具体内容如下

// 
// OrderListDownMenu.h 
 
#import  
 
@protocol OrderListDownMenuDelegate  
 
- (void)OrderListDownMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
 
@end 
 
typedef void(^Dismiss)(void); 
 
@interface OrderListDownMenu : UIView 
 
@property (nonatomic, strong) UITableView *tableView; 
@property (nonatomic, assign) id delegate; 
@property (nonatomic, strong) NSArray *arrData; 
@property (nonatomic, strong) NSArray *arrImgName; 
@property (nonatomic, copy) Dismiss dismiss; 
 
- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight; 
 
- (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion; 
 
@end 


#import "OrderListDownMenu.h" 
 
#define TopToView 63.0f 
#define rightToView kScreenWidth - 15.0f 
#define LeftToView kScreenWidth - 145.0 - 10.0f 
#define CellLineEdgeInsets UIEdgeInsetsMake(0, -80, 0, 0) 
#define kScreenWidth    [UIScreen mainScreen].bounds.size.width 
#define kScreenHeight    [UIScreen mainScreen].bounds.size.height 
 
@interface OrderListDownMenu() 
 
@property (nonatomic, assign) CGPoint origin; 
@property (nonatomic, assign) CGFloat rowHeight; 
 
@end 
 
@implementation OrderListDownMenu 
 
- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight { 
   
  self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; 
  if (self) { 
    if (rowHeight <= 0) { 
      rowHeight = 50; 
    } 
     
    // 设置背景颜色 
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]; 
    self.origin = origin; 
    self.rowHeight = rowHeight; 
    self.arrData = [dataArr copy]; 
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + LeftToView, origin.y + TopToView, width, rowHeight * dataArr.count) style:UITableViewStylePlain]; 
    _tableView.dataSource = self; 
    _tableView.delegate = self; 
    [self addSubview:_tableView]; 
     
    _tableView.backgroundColor = [UIColor whiteColor]; 
    _tableView.layer.cornerRadius = 2; 
    _tableView.bounces = NO; 
    _tableView.layer.cornerRadius = 8; 
    _tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1]; 
    
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone; 
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 
     
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { 
      [self.tableView setSeparatorInset:CellLineEdgeInsets]; 
    } 
     
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { 
      [self.tableView setLayoutMargins:CellLineEdgeInsets]; 
    } 
  } 
  return self; 
} 
 
- (void)layoutSubviews { 
  [super layoutSubviews]; 
} 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
  return self.arrData.count; 
} 
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
  return self.rowHeight; 
} 
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
   
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
  cell.textLabel.textColor = THEME_COLOR_GRAY_1; 
  cell.textLabel.fOnt= [UIFont systemFontOfSize:15]; 
  cell.textLabel.text = self.arrData[indexPath.row]; 
   
  if (self.arrImgName.count > indexPath.row) { 
    cell.imageView.image = [UIImage imageNamed:self.arrImgName[indexPath.row]]; 
    cell.imageView.cOntentMode= UIViewContentModeScaleAspectFit; 
  } 
   
  UILabel *label = [[UILabel alloc] init]; 
  label.frame = CGRectMake(0, 49, _tableView.frame.size.width, 0.5); 
  label.backgroundColor = THEME_SEPARATOR_COLOR; 
  [cell.contentView addSubview:label]; 
   
  return cell; 
} 
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
   
  if([self.delegate respondsToSelector:@selector(OrderListDownMenu:didSelectRowAtIndexPath:)]){ 
    [self.delegate OrderListDownMenu:tableView didSelectRowAtIndexPath:indexPath]; 
  } 
   
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
  [self dismissWithCompletion:nil]; 
} 
 
- (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion { 
   
  __weak __typeof(self) weakSelf = self; 
  [UIView animateWithDuration:0.2 animations:^{ 
    weakSelf.alpha = 0; 
    weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView + 145, weakSelf.origin.y + TopToView, 0, 0); 
  } completion:^(BOOL finished) { 
    [weakSelf removeFromSuperview]; 
    if (completion) { 
      completion(weakSelf); 
    } 
    if (weakSelf.dismiss) { 
      weakSelf.dismiss(); 
    } 
  }]; 
} 
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
   
  UITouch *touch = [touches anyObject]; 
  if (![touch.view isEqual:self.tableView]) { 
    [self dismissWithCompletion:nil]; 
  } 
} 
 
- (void)drawRect:(CGRect)rect { 
   
  //[colors[serie] setFill]; 
   
  //拿到当前视图准备好的画板 
   
  CGContextRef cOntext= UIGraphicsGetCurrentContext(); 
   
  //利用path进行绘制三角形 
   
  CGContextBeginPath(context);//标记 
   
  CGContextMoveToPoint(context, 
             rightToView - 13, 53);//设置起点 
   
  CGContextAddLineToPoint(context, 
              rightToView - 21, TopToView); 
   
  CGContextAddLineToPoint(context, 
              rightToView - 4, TopToView); 
   
  CGContextClosePath(context);//路径结束标志,不写默认封闭 
   
 
  [self.tableView.backgroundColor setFill]; //设置填充色 
   
  [self.tableView.backgroundColor setStroke]; //设置边框颜色 
   
  CGContextDrawPath(context, 
           kCGPathFillStroke);//绘制路径path 
} 
 
@end 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒的应用方法及特点。ELISA法作为一项新技术在免疫诊断中的应用范围不断扩大,不仅适用于多种病原微生物引起的传染病、非传染病的免疫诊断,也可用于大/小分子抗原的定量检测。ELISA法具有灵敏、特异、简单、快速、稳定及易于自动化操作等特点,是一种早期诊断的良好方法,也可用于血清流行病学调查。MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒使用方法包括对血清和血浆的操作要求。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文介绍了多因子选股模型在实际中的构建步骤,包括风险源分析、因子筛选和体系构建,并进行了模拟实证回测。在风险源分析中,从宏观、行业、公司和特殊因素四个角度分析了影响资产价格的因素。具体包括宏观经济运行和宏经济政策对证券市场的影响,以及行业类型、行业生命周期和行业政策对股票价格的影响。 ... [详细]
  • 宁德时代与第四范式达成合作,将利用第四范式的AI技术,打造规模化的人工智能平台,并将AI技术融入电池生产线。通过全流程AI技术和低门槛的AI生产工具,宁德时代实现了对生产线数据的实时分析与决策。第四范式是一家人工智能技术与服务提供商,其先知平台降低了AI在各行业内的应用门槛。宁德时代是国内具备国际竞争力的动力电池制造商之一,专注于新能源汽车动力电池系统、储能系统的研发、生产和销售。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 解决Cydia数据库错误:could not open file /var/lib/dpkg/status 的方法
    本文介绍了解决iOS系统中Cydia数据库错误的方法。通过使用苹果电脑上的Impactor工具和NewTerm软件,以及ifunbox工具和终端命令,可以解决该问题。具体步骤包括下载所需工具、连接手机到电脑、安装NewTerm、下载ifunbox并注册Dropbox账号、下载并解压lib.zip文件、将lib文件夹拖入Books文件夹中,并将lib文件夹拷贝到/var/目录下。以上方法适用于已经越狱且出现Cydia数据库错误的iPhone手机。 ... [详细]
author-avatar
与XUE一起中意JJ的小杨杨_746
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有