block的學習的心得,block學習心得

來源:互聯網
上載者:User

block的學習的心得,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];

                           }

    

        };

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

寫的比較亂額,我還是一個新手,裡面有錯誤的地方還請不吝賜教,

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.