您好我是代表团的新手,我所拥有的是一个带有自定义表格视图单元格的TableView,其中包含委托协议。
当我点击自定义视图单元的子视图按钮时,它将启动一个将值传递给我的ViewControllers方法的事件。
TableView位于视图控制器内部。
customviewcell工作正常我甚至在点击按钮时记录它并且工作正常但是当它不想进入名为self.delegate respondsToSelector:@selector(btnEditParentLabelText:)
这是我的customviewcell.h
// ParentTableCell.h
#import <UIKit/UIKit.h>
@protocol ParentTableCellDelegate <NSObject>
@optional
- (void)btnEditParentLabelText:(NSString *)amountParentLabel;
@end
@interface ParentTableCell : UITableViewCell
@property (strong,nonatomic) IBOutlet UITextField *parentLabel;
@property (nonatomic, weak) id <ParentTableCellDelegate>delegate;
-(IBAction)btnEditParentLabel:(id)sender;
@end
customviewcell.m(并非所有代码都只是授权所需的代码)
// ParentTableCell.m
#import "ParentTableCell.h"
-(IBAction)btnEditParentLabel:(id)sender{
NSLog(@"click btn");
if ([self.delegate respondsToSelector:@selector(btnEditParentLabelText:)]) {
NSLog(@"Inside");
[self.delegate btnEditParentLabelText:@"test"];
}
};
@end
这是我的视图控制器,我实现TableParentCellDelegate并包含TableView
// PositionViewController.h
#import <UIKit/UIKit.h>
#import "ParentTableCell.h"
@interface PositionViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,ParentTableCellDelegate>
{
UIAlertView *addPostionpopup;
}
#define addPositionAlert 1
#define deletePositionAlert 2
@property(nonatomic,strong) IBOutlet UITableView *positionTable;
- (IBAction)btnAddPosition:(id)sender;
@end
它的m文件只是放置了用于委托的方法:
- (void)btnEditParentLabelText:(NSString *)amountParentLabel{
NSLog(@">>> %@", amountParentLabel);
}
我的实施中有什么问题吗?
由于
答案 0 :(得分:4)
编辑 PositionViewController.m 中的代码,如下所示。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Other code to draw the cell
cell.delegate = self;
return cell;
}
答案 1 :(得分:0)
这样做
// ParentTableCell.h
#import <UIKit/UIKit.h>
@protocol ParentTableCellDelegate <NSObject>
@optional
- (void)btnEditParentLabelText:(NSString *)amountParentLabel;
@end
@interface ParentTableCell : UITableViewCell
@property (strong,nonatomic) IBOutlet UITextField *parentLabel;
@property (nonatomic, assign) id <ParentTableCellDelegate>delegate;//made assign for delegates
-(IBAction)btnEditParentLabel:(id)sender;
@end
.m file of ur custom cell
@synthesize delegate; //synthesize it
// PositionViewController.h
#import <UIKit/UIKit.h>
#import "ParentTableCell.h"
@interface PositionViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,ParentTableCellDelegate>
{
UIAlertView *addPostionpopup;
}
#define addPositionAlert 1
#define deletePositionAlert 2
@property(nonatomic,strong) IBOutlet UITableView *positionTable;
- (IBAction)btnAddPosition:(id)sender;
@end
.m file of your PositionViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//checkings and all stuffs
if(cell == nil)
{
cell //initilise
cell. delegate = self; // this is important
}
return cell;
}