Delegates and blocks are two mechanisms of implementing callbacks on iOS. Block can basically replace the function of the delegate, and the implementation is relatively concise, it is recommended to use the block of the place do not use the delegation.
Achieve results
The first step, custom Customcell
1 #import < foundation /foundation.h > Span style= "color: #008080;" > 2 3 @interface Customcell: UITableViewCell 4 5 @property (strong, nonatomic) Iboutlet UILabel *labname; @property (copy,nonatomic) void (^buygoods) ( NSString *); 8 -( ibaction) buttonbuypressed: (ID) sender; 10 @end
1 #import "CustomCell.h" 2 3 @implementation Customcell 4 5 6 7 -(Ibaction) buttonbuypressed: (ID) Sender { 8 9 nsstring *goodname=_ Labname.text; Ten _buygoods (goodname); One A } @end
In the custom.h file there are
@property (copy, nonatomic) void(^buygoods) (nsstring *);
Declares a block variable
Callback inside the CUSTOM.M file
nsstring *goodname=_labname. text;
_buygoods (goodname);
See below the implementation of the class inside
1 #import "ViewController.h"2 #import "CustomCell.h"3 4 @interface Viewcontroller ()5 6 @end7 8 @implementation Viewcontroller9 Ten -(void) Viewdidload One { A [Super Viewdidload]; - additional setup after loading the view, typically from a nib. - the _tabgoods.delegate=self; - _tabgoods.datasource=self; - } - + -(void) didreceivememorywarning - { + [Super didreceivememorywarning]; A //Dispose of any resources, can be recreated. at } - - -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath - { - static NSString *cellidentifier = @ "Customcell"; - Customcell *cell = (customcell*) [TableView dequeuereusablecellwithidentifier:cellidentifier]; in if (cell = = nil) { - Nsarray *nib = [[NSBundle mainbundle] loadnibnamed:@ "Customcell" owner:self Options:nil]; to cell = [nib objectatindex:0]; + } - Cell.labName.text = [nsstring stringwithformat:@ "Product name%d", Indexpath.row]; the __block nsstring *tipmsg=cell.labname.text; * cell. buygoods=^ (NSString *str) { $ uialertview *alertview=[[uialertview alloc]initwithtitle:@ "prompt content" message:tipmsg Delegate:nil cancelButtonTitle : @ "Cancel" otherbuttontitles:nil, nil];Panax Notoginseng [Alertview show]; - }; the + return cell; A } the + -(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ - return 1; $ } $ - -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ - return ten; the } - Wuyi@end
Inside the viewcontroller.m file
__block nsstring *tipmsg=cell. Labname. text;
Cell. buygoods=^ (nsstring *str) {
Uialertview *alertview=[[uialertview alloc]initwithtitle:@ " prompt content " message:tipmsg delegate:nil cancelbuttontitle:@ " cancel " otherbuttontitles:Nil , Nil];
[Alertview show];
};
Implement block inside block code, __block nsstring *tipmsg=cell. Labname. this line guarantees that the variable can be used in block code blocks ....
Run:
iOS Development Block (ii) Implementation delegation