如何从父类调用方法到它的子类?

在我的主视图控制器中有一个如下方法:

-(void)updateCartItem { } 

我想在按钮操作方法下将其调用到uitableviewcell类:

 - (IBAction)Cart:(id)sender { [self updateCartItem];//like this you can call parent method which contain your VC.m file } 

请提前帮助我…

您可以使用Delegate或代码块来执行此操作,我将使用示例发布两种方式进行说明


代表方法

1 – 声明您的单元格委托

我们的示例单元格将被称为CustomTableViewCell

 @protocol CustomCellDelegate -(void)executeAction; @end 

并将您的委托添加到您的单元格声明中,必须是弱的以避免保留周期

 @property (weak) id cellDelegate; 

2 – 在Cell Action中执行您的委托操作

 - (IBAction)Cart:(id)sender { [[self cellDelegate] executeAction]; } 

3 – 让你的UIViewController实现CustomCell的CustomCellDelegate

 @interface ViewController ()  -(void)executeAction { [self updateCartItem]; } 

4 – 使您的UIViewController成为CustomCell的Delegate ,调整您的cellForRowAtIndexPath方法

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CustomCell"; CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath]; cell.cellDelegate = self; return cell; } 

完整代码

CustomTableViewCell.h

 #import  @protocol CustomCellDelegate -(void)executeAction; @end @interface CustomTableViewCell : UITableViewCell @property (weak) id cellDelegate; @end 

CustomTableViewCell.m

 #import "CustomTableViewCell.h" @implementation CustomTableViewCell - (IBAction)Cart:(id)sender { [[self cellDelegate] executeAction]; } @end 

ViewController.m

 #import "ViewController.h" #import "CustomTableViewCell.h" @interface ViewController ()  @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [_tableView setDelegate:self]; [_tableView setDataSource:self]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CustomCell"; CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath]; cell.cellDelegate = self; return cell; } -(void)updateCartItem { //Whatever you need to do here } -(void)executeAction { [self updateCartItem]; } @end 

代码块方法

1 – 在Customcell中声明您的actionBlock

我们的示例单元格将被称为CustomCell

  @property void(^actionBlock)(void); 

2 – 在“单元格操作”中执行操作块

 - (IBAction)Cart:(id)sender { [self actionBlock]; } 

3 – 设置细胞块操作,调整cellForRowAtIndexPath方法

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CustomCell"; CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath]; __weak ViewController *weakSelf = self; cell.actionBlock = ^{ [weakSelf updateCartItem]; }; return cell; } 

完整代码

CustomTableViewCell.h

 #import  @interface CustomTableViewCell : UITableViewCell @property void(^actionBlock)(void); @end 

CustomTableViewCell.m

 #import "CustomTableViewCell.h" @implementation CustomTableViewCell - (IBAction)Cart:(id)sender { [self actionBlock]; } @end 

ViewController.m

 #import "ViewController.h" #import "CustomTableViewCell.h" @interface ViewController ()  @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [_tableView setDelegate:self]; [_tableView setDataSource:self]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CustomCell"; CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath]; __weak ViewController *weakSelf = self; cell.actionBlock = ^{ [weakSelf updateCartItem]; }; return cell; } -(void)updateCartItem { //Whatever you need to do here } @end