2012-04-10 43 views
0

使用iOS5。我將UIButton對象拖到自定義靜態單元格內。由於我沒有使用動態屬性,因此我沒有使用數據源模板。事情是,當在單元格內按下UIButton時,我無法觸發任何事件。我運行的是folioing代碼在我的表視圖控制器:在自定義靜態單元格內按UIButton時沒有采取行動

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSIndexPath *myIP = [NSIndexPath indexPathForRow:1 inSection:1]; 
    UITableViewCell *cell = [medicareTableView cellForRowAtIndexPath:myIP]; 
    UIButton *stepNumberOfChildren = (UIButton *)[cell viewWithTag:222]; 

    [stepNumberOfChildren addTarget:self action:@selector(dontTouchMe:) forControlEvents:UIControlEventTouchUpInside]; 

} 
- (void)dontTouchMe:(id)sender { 
    NSLog(@"Touched Button %i",((UIButton *)sender).tag); 
} 

運行的應用,按UIButton的,它不記錄任何東西......我在做什麼錯?

+0

嘗試在此行更改1到0 [NSIndexPath indexPathForRow:1 inSection:1];' – 2012-04-10 10:50:33

+0

在viewDidLoad中,您可能無法使用'cellForRowAtIndexPath'獲取tableviewcell – DivineDesert 2012-04-10 10:57:15

回答

2

您的插座連接到dontTouchMe功能(TouchUpInside)

+0

謝謝!我的插座沒有連接。出於某種原因,NSLog沒有被啓動,但是按鈕事件確實被推送,因爲我可以調用其他調用。 – Pupillam 2012-04-10 11:06:42

+1

請點擊接受的答案。將不勝感激。 – Satish 2012-04-10 11:34:42

0

我希望要的目標僅添加到行的單元格= 1個節= 1

所以代碼如下,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexpath.row==1&&indexpath.section==1) 
    { 
    //add the button target 
    } 
} 
相關問題