zoukankan      html  css  js  c++  java
  • block的学习的心得

    额我主要说它的属性,和在添加cell的事件的时候如果使用block实现点击的事件。

    block就是一个传值回调的一个过程,它能降低耦合度。block看似和对象没有多大的关系。但是里面的block却执行了关于对象的事件。他的语法那些视频上都有,这里就不多说了。

    但是有这个3点。1.在block中引用局部的变量时会变成常量不可以修改 ,要想修改时必须是__block修饰时才可以修改 2.在内存方面还是局部变量会retain,__block修饰时不会retain 且block声明全局变量时,我们应该调用block的copy方法。为什么要引用计数要加一,因为要想调用执行block的代码必须block代码的调用,但是有可能整个代码执行完都没调用对象已经被销毁了如果以后调用的时候就可能没有什么用。3.如果在block中的代码中的对象是不可以销毁的这就造成内存的泄露,一个简单记得方法只要在block中用到对象都都要在外面用和它同一个对象类型用__block修饰。就是这么多。

    分享一个我添加cell的事件的时候如果使用block实现点击的事件;

    #import <UIKit/UIKit.h>

    //传递的是用户点击cell的哪一行

    typedef  void (^cellButton)(NSInteger indexPath);

    @interface rootTableViewCell : UITableViewCell

    //必须吧UITableView的tableView给UITableViewCell对象的属性tableView。这是就可以获得用户点击的cell的第几行。

    @property (weak, nonatomic)UITableView *tableView;

    @property(nonatomic,copy)cellButton cellblock;

    @property (nonatomic) UIButton *myButton;

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

    @end

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

        self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];

        if (self){

            _myButton=[[UIButton alloc]init];

            _myButton.frame=CGRectMake(0, 0, 375, 100);

            [_myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

            

            [self addSubview:_myButton];

            

            

        }

        

        return self;

    }

    - (void)buttonAction:(id)sender {

    //获取行数并且吧这个行数传给cellblock这个block块调用。

        NSIndexPath *indexpath=[[self tableView] indexPathForCell:self];

        if (_cellblock) {

            _cellblock(indexpath.row);

        }

           }

     

     -----------------------------------------------------------------

    //在定制的UITableViewCell中,如果需要对cell中的控件添加事件响应,就要想办法把cell的indexPath传递给响应函数。下面是一个相对方便且耦合度低的方法。UICollectionView同样适用。在MyUITableViewCell中添加一个指向UITableView的指针,需要获取indexPath时,通过指针向tableView查询。在MyUITableViewCell中添加一个block属性,在实例化MyUITableViewCell时,给block赋值。在MyUITableViewCell的实现中响应MyButton的点击事件,在响应函数中调用block。

            cell.tableView = tableView;

     

            cell.cellblock = ^(NSInteger index){

                if (index==0) {

                    mainViewController *mainView=[[mainViewController alloc]init];

                    [self.navigationController pushViewController:mainView animated:YES];

                               }

        

            };

     -------------------------------------------------------------------------

    写的比较乱额,我还是一个新手,里面有错误的地方还请不吝赐教,

  • 相关阅读:
    Vmware
    Centos8
    Jmeter系列(37)- 详解 ForEach控制器
    【解决】k8s 1.15.2 镜像下载方案
    【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter
    【解决】venv 的名字在 zsh prompt 中不显示
    【解决】could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
    《Linux 性能优化实战—倪朋飞 》学习笔记 CPU 篇
    【解决】MongoDB 线上业务处理,数据去重脚本实现
    【解决】docker 容器中 consul集群问题处理
  • 原文地址:https://www.cnblogs.com/xiangruru/p/4948363.html
Copyright © 2011-2022 走看看