在我的主视图 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 - 让你的
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;
}
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/