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

时间:2018-02-12 06:26:30

标签: ios objective-c uitableview

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

-(void)updateCartItem {

}

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

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

请提前帮助我...

1 个答案:

答案 0 :(得分:4)

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

代表方法

1 - 声明您的手机代理

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

@protocol CustomCellDelegate
-(void)executeAction;
@end

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

@property (weak) id<CustomCellDelegate> cellDelegate;

2 - 在您的单元格操作中执行您的委托操作

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

3 - 让您的UIViewController实现CustomCell的CustomCellDelegate

@interface ViewController () <UITableViewDataSource,UITableViewDelegate,CustomCellDelegate>

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

4 - 将UIViewController Delegate作为您的CustomCell 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; } 方法

#import <UIKit/UIKit.h>

@protocol CustomCellDelegate
-(void)executeAction;
@end

@interface CustomTableViewCell : UITableViewCell

@property (weak) id<CustomCellDelegate> cellDelegate;

@end
  

完整代码

<强> CustomTableViewCell.h

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

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

@end

<强> CustomTableViewCell.m

#import "ViewController.h"
#import "CustomTableViewCell.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate,CustomCellDelegate>

@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

<强> ViewController.m

 @property void(^actionBlock)(void);

代码块方法

1 - 在Customcell中声明您的actionBlock

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

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

2 - 在单元格操作中执行操作块

-(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;
}

3 - 设置调整cellForRowAtIndexPath方法的单元格块操作

#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell

@property void(^actionBlock)(void);

@end
  

完整代码

<强> CustomTableViewCell.h

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

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

@end

<强> CustomTableViewCell.m

#import "ViewController.h"
#import "CustomTableViewCell.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

@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

<强> ViewController.m

{{1}}