Recently encountered a problem in the process of learning, the whole project with Uinavigationcontroller as the root controller, a table trying to use the controller of a custom UITableViewCell, the class cell has a number of custom buttons, When clicking on any of the cell buttons, you need to know which cell is currently and which button is clicked and then do the corresponding event response (change the row data, page jump, etc.), before using the agent, this time want to change a way, so chose to replace with a block, the following list of detailed steps
1. Declare the block in the custom cell header file and define the appropriate block type
#import <UIKit/UIKit.h>
@class Shopingcarttableviewcell;
Block declaration
typedef void(^reducegoodnums) (shopingcarttableviewcell *);
typedef void(^addgoodnums) (shopingcarttableviewcell *);
typedef void(^selectgoods) (shopingcarttableviewcell *);
@interface Shopingcarttableviewcell: UITableViewCell
The corresponding block variable definition
@property (strong,nonatomic) reducegoodnums reducegoodnumblock;
@property (strong,nonatomic) addgoodnums addgoodnumblock;
@property (strong,nonatomic) selectgoods selectgoodblock;
@end
2. To populate the table with the custom cell, implement the Block function, here to add the number of goods as an example, reduce the number of goods and the number of selected items
// add number of items block
Cell. Addgoodnumblock = ^ (shopingcarttableviewcell *cell)
{
nsindexpath *indexpath = [self. TableViewIndexpathforcell: cell];
nsdictionary *goodsdic = [self. DataSourceobjectatindex: Indexpath. section];
nsarray *goodsarray = [Goodsdic objectforkey:@ "Array"];
Self. good = [Goodsarrayobjectatindex: Indexpath. Row];
Self. good. num = [cell. NUMTF. text Intvalue] +1;
// increase the number of items
if ([selfalertnum])
{
Cell. NUMTF. text = [nsstringstringwithformat:@ "%d", [cell]. NUMTF. text Intvalue] + 1];
}
// Modify the total amount
if (Cell. checkboxbtn. selected)
{
[ self alertselecedgoodnum];
// Calculate price
[ self alertsum];
Indexpath = [nsindexpath indexpathforrow:0 insection:self. DataSource. Count];
nsarray *indexarray=[nsarrayarraywithobject: Indexpath];
[self. TableView reloadrowsatindexpaths: Indexarray withrowanimation:uitableviewrowanimationautomatic ];
}
};
IOS Block Usage