2011-03-24 40 views
1

我有一個問題...如何捕獲在自定義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我想趕上事件,當用戶改變開關的狀態。我怎樣才能做到這一點 ?

回答

5

你必須寫值變化的方法如下:

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

然後,你必須實現委託

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

,那麼你必須做出對象ID代表和合成(非原子,分配)的性質和方法如下:

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

通過這種方式,你可以得到通知st在視圖控制器中吃了變化。

+0

某些警告顯示!!! Whot我必須杜? – 2011-03-24 07:31:24

3

設置交換機的目標操作以通知更改。

要在創建單元格做這個調用

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

+0

您可能還需要設置開關的標籤屬性,以便您可以識別事件處理程序中的特定表格視圖單元。 – hennes 2011-03-24 06:55:41

+0

對不起,但我如何設置目標行動???到我的customCell? – 2011-03-24 07:08:31

+0

在您的表格視圖數據源中,您需要類似'[cell.cellSwitch addTarget:self action:@selector(switchToggled :) forControlEvents:UIControlEventValueChanged]' – Undeadlegion 2011-03-24 08:37:30

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

但herte我得到worning:「-respondToSelector」 在協議沒有找到。

+0

寫這個insted的最後一個功能!它的作品很棒! - (void)valueChange:(id)sender { \t [delegate valueChangeNotify:sender]; } – 2011-03-24 13:22:32

相關問題