如何捕获自定义tableViewCell中更改的开关事件? [英] How to catch event of switch changed in custom tableViewCell?
本文介绍了如何捕获自定义tableViewCell中更改的开关事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题...
我有Custom TableViewCell类:
I have Custom TableViewCell class:
// Class for Custom Table View Cell.
@interface CustomTableViewCell : UITableViewCell {
// Title of the cell.
UILabel* cellTitle;
// Switch of the cell.
UISwitch* cellSwitch;
}
如何在我的自定义UITableViewCell中看到我具有标签和开关控制器.
How you can see in my custom UITableViewCell I have Label and Switch controller.
- (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中使用自定义单元格时,我想在用户更改 switch 的状态时捕获事件.我该怎么办?
Now when I use my custom cell in TableView I want to catch event when user change the state of switch. How can I do that ?
推荐答案
您必须编写值更改方法,如下所示:
You have to write method for value change as below:
[cellSwitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
然后您必须实现委托
@protocol SwitchDelegate <NSObject>
- (void)valueChangeNotify:(id)sender;
@end
然后,您必须使用(非原子,分配)和方法正确地进行对象id的委托和合成:
then you have to make object id delegate and synthesis with (nonatomic, assign) propertly and method as below:
- (void)valueChange:(id)sender{
if ([delegate respondToSelector:@selector(valueChangeNotify:)])
[delegate valueChangeNotify:sender];
}
通过这种方式,您可以在视图控制器中获取通知状态更改.
By this way, you can get notify state change in view controller.
这篇关于如何捕获自定义tableViewCell中更改的开关事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文