2015-05-13 38 views
0

我有tableView與自定義單元格有一個segmentControl。我需要爲每個細胞捕捉狀態她的segmentControl。我已經頭腦轉動了,我找不到錯誤。你可以幫我嗎? h。Objective-C(發件人)

h。文件segmentControl:

@property (nonatomic) NSIndexPath *button_indexPath; 

米文件細胞:

- (IBAction)changed1:(id)sender{ 
     NSInteger i = self.segmentMeating.button_indexPath.row; } 

米。文件與表格視圖:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     cell.segmentMeating.button_indexPath = indexPath; 
     [cell.segmentMeating addTarget:nil action:@selector(changed1:) forControlEvents:UIControlEventValueChanged]; 
+0

爲什麼addTarget零? – vichevstefan

+0

我想你可能不得不實現一些代表來獲取每一行的每個分段控件 – Claudio

+0

還要記住,表視圖重用單元格。每次調用'addTarget:action:'你再添加一個監聽器 – Azat

回答

0

在你的m。與表視圖文件時,segmentMeating目標不應該是tableView(個體經營),但cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     cell.segmentMeating.button_indexPath = indexPath; 
     [cell.segmentMeating addTarget:cell action:@selector(changed1:) forControlEvents:UIControlEventValueChanged]; 

因爲,你的目標是行動的Cell class.T

+0

是的,它是!感謝您的幫助! – mike

0

在這種情況下,您需要使用協議和委託方法。 在你cell.m創建委託名稱:

- (void)tableView:(UITableView *)tableView segmentedControl:(UISegmentedControl *)segmentControl changedValueAtIndex:(NSIndexPath *)indexPath; 

然後把它在你cell.m

- (IBAction)changed1:(id)sender 

然後實現它在你tableview.m,你會得到修改的值,因爲你有在param中的segmentControl。

+0

感謝您的幫助,它在iOS8上工作,在iOS7上無法工作。我仍然需要支持早期版本。 – mike

0

所有你需要的是UISegmentedControlNSIndexPath,通過valueChanged事件觸發你的changed1函數,然後你就可以確切地知道動作從哪裏開始。沒有必要像你在做的那樣將indexPath存儲爲一個變量。

// Place this in your viewController 
- (IBAction)changed1:(id)sender{ 
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
    NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint]; 
    // Now you know exactly which segmented control actually fired this action. 
} 

// Set self as the target 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    // 
    [cell.segmentMeating addTarget:self action:@selector(changed1:) forControlEvents:UIControlEventValueChanged]; 
    // 
} 
+0

我可以得到發生變化的單元格編號segmentControl,但是我無法使操作獲取此segmentControl的狀態。 – mike

+0

你是什麼意思,你不能導致行動? changed1(UIControlEventValueChanged)事件是否正在觸發?如果沒有,你是否設置了addTarget:self? –

+0

它在iOS8上工作,在iOS7上不工作。在執行後的ios7中change1得到它:[ViewController changed1:]:無法識別的選擇器發送到實例 – mike