简体   繁体   English

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

[英]How to catch event of switch changed in custom tableViewCell?

I have a question ... 我有个问题 ...

I have Custom TableViewCell class: 我有Custom TableViewCell类:

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

How you can see in my custom UITableViewCell I have Label and Switch controller. 您如何在我的自定义UITableViewCell中看到具有Label和Switch控制器。

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

Now when I use my custom cell in TableView I want to catch event when user change the state of switch . 现在,当我在TableView中使用自定义单元格时,我想在用户更改switch状态时捕获事件。 How can I do that ? 我怎样才能做到这一点 ?

You have to write method for value change as below: 您必须编写更改值的方法,如下所示:

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

Then you have to implement delegate 然后,您必须实现委托

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

then you have to make object id delegate and synthesis with (nonatomic, assign) propertly and method as below: 那么您必须正确地使用(非原子,分配)和方法进行对象id的委托和合成:

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

By this way, you can get notify state change in view controller. 这样,您可以在视图控制器中获取通知状态更改。

Set up a target action for the switch to be notified of changes. 设置目标操作,以便将更改通知给交换机。

To do this call 拨打电话

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

when you create the cell. 当您创建单元格时。

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

but herte I get worning: "-respondToSelector" not find in protocol. 但继承人我很讨厌: “- respondToSelector ”在协议中找不到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM