ios - 如何从父类调用一个方法到它的子类?

标签 ios objective-c uitableview

在我的主视图 Controller 中有如下方法:

-(void)updateCartItem {

}

我想在按钮操作方法下将它调用到 uitableviewcell 类中:
- (IBAction)Cart:(id)sender {
  [self updateCartItem];//like this you can call parent method which contain your VC.m file
}

请帮助我提前谢谢...

最佳答案

您可以使用 Delegate或代码块来执行此操作,我将通过示例发布两种方式以供您澄清

委托(delegate)方法

1 - 声明您的单元代表

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

@protocol CustomCellDelegate
-(void)executeAction;
@end

并将您的委托(delegate)添加到您的单元格声明中,必须很弱以避免保留周期
@property (weak) id<CustomCellDelegate> cellDelegate;

2 - 在您的单元格操作中执行您的委托(delegate)操作
- (IBAction)Cart:(id)sender {
    [[self cellDelegate] executeAction];
}

3 - 让你的 UIViewController 实现你的 CustomCell 的 CustomCellDelegate
@interface ViewController () <UITableViewDataSource,UITableViewDelegate,CustomCellDelegate>

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

4 - 让你的UIViewControllerDelegate您的 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;
}

Full Code



CustomTableViewCell.h
#import <UIKit/UIKit.h>

@protocol CustomCellDelegate
-(void)executeAction;
@end

@interface CustomTableViewCell : UITableViewCell

@property (weak) id<CustomCellDelegate> 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 () <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

代码块方法

1 - 在您的自定义单元中声明您的 actionBlock

我们的示例单元格将被称为 CustomCell
 @property void(^actionBlock)(void);

2 - 在您的单元格操作中执行您的操作 block
- (IBAction)Cart:(id)sender {
    [self actionBlock];
}

3 - 设置您的单元格 block 操作调整您的 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;
}

Full Code



CustomTableViewCell.h
#import <UIKit/UIKit.h>

@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 () <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

关于ios - 如何从父类调用一个方法到它的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740651/

相关文章:

objective-c - GCC_NO_COMMON_BLOCKS 有什么用?

android - Phonegap/Cordova - 构建 Apk

iOS 9 与第三方 Mandrill/MailChimp 深度链接

objective-c - 使用 Ruby 而不是 AppleScript 使 Cocoa 应用程序可编写脚本

ios - 5.0之前的iOS版本上的用户定义的运行时属性

uitableview - 如何快速获取所选行的textLabel?

iphone - Objective-C - SecItemAdd 有错误 :EXC_BAD_ACCESS (first time) and errSecDuplicateItem (second time)

iOS UITextView 生成所选文本的屏幕截图 - 是否可以使用 TextKit 或任何其他方法?

uitableview - 在自定义 UITableViewCell 类中创建 UIImageView (Swift)

ios - 选择和取消选择表格 View 单元格