如何在自定义tableViewCell中捕获切换事件?

时间:2011-03-24 06:44:17

标签: iphone ios events uitableview

我有一个问题......

我有Custom TableViewCell类:

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

如何在我的自定义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;
}

现在,当我在TableView中使用自定义单元格时,我希望在用户更改开关的状态时捕获事件。我怎么能这样做?

3 个答案:

答案 0 :(得分:5)

您必须编写值更改方法,如下所示:

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

然后你必须实现委托

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

然后你必须使用(nonatomic,assign)属性和方法使对象id委托和合成如下:

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

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

答案 1 :(得分:3)

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

要执行此操作

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

创建单元格时。

答案 2 :(得分:1)

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

但在这里我收到警告:“ - respondsToSelector”在协议中找不到。