2013-08-28 83 views
1

嗨,我是新的委派,我有一个TableView与自定义表格视图单元格包含委托协议。自定义委托ios respondsToSelector不工作自定义表格单元格

当我点击自定义视图单元格的子视图按钮时,它会触发一个事件,该事件会将值传递给我的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 

这里是我的视图控制器,我实现TablePa rentCellDelegate和包含的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

你设置你的控制器作为该小区的委托? – alex

+0

您是否将委派分配给您的单元格的委托属性? – Jeremy

+0

啊,这就是什么cell.delefat =自我?你能解释为什么你需要这个?它是否在其中实施协议? –

回答

4

编辑在PositionViewController.m如下面的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Other code to draw the cell 
    cell.delegate = self; 
    return cell; 
} 
+0

谢谢!你能解释为什么这需要? –

+0

委托是一个自定义对象,应该监听来自tableviewcell的调用。所以你应该提到PositionViewController是委托来按钮点击的单元格。 –

+0

阅读本指南。 https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11-SW1 –

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


+0

感谢这也是有效的,但唯一的区别在于代表。它也可以不合成它,它有什么区别?你们俩都是对的,但我不能选择2回答抱歉。不能给代表呢。再次感谢! –

+0

可能会发现一个警告:“autosysnthesized property delegate将使用sysnthecsized实例变量_delegate ....”来摆脱它,还有一件事情让委托使用assign。欲了解更多信息,此检查听到http://stackoverflow.com/questions/7753841/recommended-way-to-declare-delegate-properties-with-arc –