• 实现下拉弹出视图和Block的简单实现


    实现效果如下:

    实现代码如下:

      1 @interface ViewController ()<UIViewControllerTransitioningDelegate>
      2 
      3  
      4 
      5 {
      6 
      7     
      8 
      9     UILabel *optionLabel;   //显示选中值得label
     10 
     11     BOOL isPresentingOrDismissingOption;
     12 
     13     UIView *buildingOptionView;  //选择的视图
     14 
     15     
     16 
     17     
     18 
     19 }
     20 
     21 @end
     22 
     23  
     24 
     25 @implementation ViewController
     26 
     27  
     28 
     29 - (void)viewDidLoad {
     30 
     31     [super viewDidLoad];
     32 
     33  
     34 
     35     self.view.backgroundColor = [UIColor greenColor];
     36 
     37     buildingOptionView = [[UIView alloc] initWithFrame:CGRectMake(70, 100, 200, 50)];
     38 
     39     [self.view addSubview:buildingOptionView];
     40 
     41    
     42 
     43     buildingOptionView.backgroundColor = [UIColor redColor];
     44 
     45     buildingOptionView.layer.cornerRadius = 8;
     46 
     47     buildingOptionView.clipsToBounds = YES;
     48 
     49     UIGestureRecognizer *tapOnBuildingOpetionView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBuildingOpetionView:)];
     50 
     51     [buildingOptionView addGestureRecognizer:tapOnBuildingOpetionView];
     52 
     53     
     54 
     55     optionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, buildingOptionView.width - 40, 50)];
     56 
     57     optionLabel.text = @"第一教学楼";
     58 
     59     [buildingOptionView addSubview:optionLabel];
     60 
     61     optionLabel.textColor = [UIColor blackColor];
     62 
     63     
     64 
     65     UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(optionLabel.right + 10,optionLabel.top + 15,20,20)];
     66 
     67     arrowImageView.image = [UIImage imageNamed:@"classroom_arrow_icon"];
     68 
     69     [buildingOptionView addSubview:arrowImageView];
     70 
     71     
     72 
     73     
     74 
     75     
     76 
     77 }
     78 
     79  
     80 
     81 //手势触发方法
     82 
     83  
     84 
     85 - (void)tapOnBuildingOpetionView:(UITapGestureRecognizer *)tap{
     86 
     87  
     88 
     89     NSLog(@"列表选择");
     90 
     91  //弹出选择列表
     92 
     93     OptionTableView *optionView = [[OptionTableView alloc]initWithFrame:CGRectMake(buildingOptionView.left + 20,buildingOptionView.bottom + 10,150,300) style:UITableViewStylePlain];
     94 
     95     optionView.buildingsArray = @[@"第一教学楼", @"第二教学楼", @"第三教学楼", @"第四教学楼", @"第五教学楼", @"第六教学楼", @"第七教学楼", @"第八教学楼"];
     96 
     97     [self.view addSubview:optionView];
     98 
     99     
    100 
    101     //block的实现,给label赋值
    102 
    103     [optionView setDismissOnBuilding:^(NSString *selectedBuilding) {
    104 
    105         
    106 
    107         optionLabel.text = selectedBuilding;
    108 
    109         
    110 
    111     }];
    112 
    113 }

    OptionTableView的实现代码如下:

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface OptionTableView : UITableView<UITableViewDataSource,UITableViewDelegate>
    4 
    5 @property (nonatomic, strong) NSArray *buildingsArray;
    6 @property (nonatomic, copy) void (^dismissOnBuilding)(NSString *building);
    7 
    8 
    9 @end
     1 static NSString *const buildingOptionCellId = @"Cell";
     2 
     3 
     4 @interface OptionTableView ()
     5 
     6 
     7 @end
     8 @implementation OptionTableView
     9 
    10 
    11 - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    12     
    13     self = [super initWithFrame:frame style:style];
    14     if (self) {
    15         
    16         self.dataSource = self;
    17         self.delegate = self;
    18         
    19         
    20         [self registerClass:[UITableViewCell class] forCellReuseIdentifier:buildingOptionCellId];
    21         self.backgroundColor = [UIColor blueColor];
    22         
    23     }
    24     return self;
    25     
    26 }
    27 
    28 #pragma mark - Table view dataSource
    29 
    30 
    31 //行数
    32 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    33 {
    34     
    35     return self.buildingsArray.count;
    36     
    37 }
    38 
    39 
    40 //创建单元格
    41 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    42 
    43 {
    44     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:buildingOptionCellId forIndexPath:indexPath];
    45     cell.textLabel.text = self.buildingsArray[indexPath.row];
    46     return cell;
    47 
    48 }
    49 
    50 #pragma mark - Table view delegate
    51 
    52 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    53     
    54     //block实现选中的值的传递
    55     
    56     if (self.dismissOnBuilding) {
    57         self.dismissOnBuilding(self.buildingsArray[indexPath.row]);
    58     }
    59     
    60     //当选择完毕表视图收起
    61     self.frame = CGRectZero;
    62 }

    写的不好,有更好的方法,欢迎大家告诉我,谢谢!!!

  • 相关阅读:
    一个Netfilter nf_conntrack流表查找的优化-为conntrack添加一个per cpu cache
    【翻译自mos文章】检查$ORACLE_HOME是否是RAC的HOME的方法以及relink RAC的Oracle binary的方法
    DVBS/S2在数字电视系统中的应用 三 (LNB介绍)
    cache数据库之表的存储结构
    jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径
    彻底搞懂oracle的标量子查询
    OpenCV学习教程入门篇&lt;一、介绍&gt;
    NYOJ 38 布线问题_(解法1 Kruskal算法)
    HTML5之WebSocket && https://zhuanlan.zhihu.com/p/23467317
    HTML5离线缓存
  • 原文地址:https://www.cnblogs.com/pengsi/p/5332197.html
Copyright © 2020-2023  润新知