Block learning experience and block learning experience

Source: Internet
Author: User

Block learning experience and block learning experience

I mainly talk about its attributes and click events when adding cell events using block.

Block is a process of callback for passing values. It can reduce coupling. Block seems to have little to do with objects. But the block in it executes the event about the object. He has syntaxes on videos. I will not talk about them here.

But there are three points. 1. when a local variable is referenced in a block, it becomes a constant and cannot be modified. to modify the variable, it must be _ block. in terms of memory, when the local variable will retain, __when the block is modified, it will not retain and when the block declares the global variable, we should call the block copy method. Why should I add one to the reference count? To call the code that executes the block, the block code must be called, however, it is possible that the call object has been destroyed after the entire code is executed. If it is called later, it may be useless. 3. if the objects in the block Code cannot be destroyed, this may cause memory leakage, A simple way to remember that all objects used in the block must be modified with _ block for the same object type. That's all.

Share a click event using block when I add a cell;

# Import <UIKit/UIKit. h>

// Transmits the row on which the user clicks the cell.

Typedef void (^ cellButton) (NSInteger indexPath );

@ Interface rootTableViewCell: UITableViewCell

//The tableView of UITableView must be set to the tableView attribute of the UITableViewCell object. This is the row number of the cell clicked by the user.

@ 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 {

// Obtain the number of rows and pass the number of rows to the cellblock block for calling.

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

If (_ cellblock ){

_ Cellblock (indexpath. row );

}

}

 

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

// In the custom UITableViewCell, if you need to add an Event Response to the control in the cell, you must try to pass the cell indexPath to the response function. The following is a relatively convenient and low coupling method. UICollectionView is also applicable. Add a pointer to UITableView in MyUITableViewCell. When you need to obtain the indexPath, use the pointer to query tableView. Add a block attribute to MyUITableViewCell and assign a value to the block when instantiating MyUITableViewCell. In the implementation of MyUITableViewCell, respond to the click event of MyButton and call block in the response function.

Cell. tableView = tableView;

 

Cell. cellblock = ^ (NSInteger index ){

If (index = 0 ){

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

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

}

};

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

I am still a newbie. Please kindly advise if there are any errors in it,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.