2012-01-10 17 views
0

在我UITableView的第二部分的UITableViewCell,我已經定義了一個UISegmentedControl的兩個按鈕組成:在操控性上UISegmentedControl點擊

if (indexPath.section == 1 && indexPath.row == 0) 
{ 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    allerRetour = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects:NSLocalizedString(@"Y aller", nil),         NSLocalizedString(@"En partir", nil), nil]]; 
    [allerRetour setFrame:CGRectMake(9.0f, 0.0f, 302.0f, 45.0f)]; 
    allerRetour.selectedSegmentIndex = 0; 
    [cell addSubview:allerRetour]; 
} 

return cell; 

現在,當我點擊兩個按鈕中的一個按鈕分段控制,我想跟蹤,所以我嘗試這在didSelectRowAtIndexPath,但它不工作(該的NSLog不會顯示,因此不拋出的事件):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.section==1 && indexPath.row==0) 
    { 
     if (allerRetour.selectedSegmentIndex == 0) 
     { 
      [email protected]"Départ"; 
      NSLog(@"Départ"); 
     } 
     else 
     { 
      [email protected]"Arrivée"; 
      NSLog(@"Arrivée"); 
     } 
    } 
} 

回答

4

點擊的實際分段控制不會導致這樣做SelectRowAtIndexPath。相反,您需要添加一個目標,如下所示:

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

並定義「action:」方法來響應事件。

- (void)action:(id)sender 

然後把該代碼的內部,如果在該行動的方法。

+0

我已經這樣做了,我看了一下UISegmentedControl的文檔,但是,沒有什麼改變,但是你的方法適用於很多其他情況,可能不是我的:) – Malloc 2012-01-10 15:26:16

+0

你是什麼意思,它不適合?你是否試圖讓這個工作在多行上?我可以幫助(如果需要,請用法語:)) – 2012-01-10 15:28:16