IOS Block Usage

Source: Internet
Author: User

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

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.