如何捕捉自定义tableViewCell中更改开关的事件?

我有个问题 …

我有自定义TableViewCell类:

// Class for Custom Table View Cell. @interface CustomTableViewCell : UITableViewCell { // Title of the cell. UILabel* cellTitle; // Switch of the cell. UISwitch* cellSwitch; } 

你如何看到我的自定义UITableViewCell我有标签和开关控制器。

 - (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum { // Get Self. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Create and initialize Title of Custom Cell. cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, (44 - TAGS_TITLE_SIZE)/2, 260, 21)]; cellTitle.backgroundColor = [UIColor clearColor]; cellTitle.opaque = NO; cellTitle.textColor = [UIColor blackColor]; cellTitle.highlightedTextColor = [UIColor whiteColor]; cellTitle.font = [UIFont boldSystemFontOfSize:TAGS_TITLE_SIZE]; cellTitle.textAlignment = UITextAlignmentLeft; // Create and Initialize Switch of Custom Cell. cellSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(185, 10, 10, 10 )]; [self.contentView addSubview:cellTitle]; [self.contentView addSubview:cellSwitch]; [cellTitle release]; [cellSwitch release]; } return self; } 

现在,当我在TableView中使用自定义单元格时,我想在用户更改开关状态时捕获事件。 我怎样才能做到这一点 ?

你必须写下价值变化的方法如下:

 [cellSwitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; 

那么你必须实现委托

 @protocol SwitchDelegate <NSObject> - (void)valueChangeNotify:(id)sender; @end 

那么你必须做对象id委托和合成(nonatomic,assign)属性和方法如下:

 - (void)valueChange:(id)sender{ if ([delegate respondToSelector:@selector(valueChangeNotify:)]) [delegate valueChangeNotify:sender]; } 

通过这种方式,您可以在视图控制器中获得通知状态更改。

为交换机设置目标操作以通知更改。

要做这个调用

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

当你创build单元格。

 @protocol SwitchDelegate - (void)valueChangeNotify:(id)sender; @end // Class for Custom Table View Cell. @interface CustomTableViewCell : UITableViewCell { // Title of the cell. UILabel* cellTitle; // Switch of the cell. UISwitch* cellSwitch; id<SwitchDelegate> delegate; } @property (nonatomic, assign) id <SwitchDelegate> delegate; @end - (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum { // Get Self. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Create and initialize Title of Custom Cell. cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, (44 - TAGS_TITLE_SIZE)/2, 260, 21)]; cellTitle.backgroundColor = [UIColor clearColor]; cellTitle.opaque = NO; cellTitle.textColor = [UIColor blackColor]; cellTitle.highlightedTextColor = [UIColor whiteColor]; cellTitle.font = [UIFont boldSystemFontOfSize:TAGS_TITLE_SIZE]; cellTitle.textAlignment = UITextAlignmentLeft; // Create and Initialize Switch of Custom Cell. cellSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(185, 10, 10, 10 )]; [cellSwitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; [self.contentView addSubview:cellTitle]; [self.contentView addSubview:cellSwitch]; [cellTitle release]; [cellSwitch release]; } return self; } -(void)valueChange:(id)sender{ if([delegate respondToSelector:@selector(valueChangeNotify:)]) [delegate valueChangeNotify:sender]; } 

但herte我得到worning: “- respondToSelector ”找不到协议。