2012-10-22 11 views
3

我有細胞內的按鈕,它有一個出口的自定義單元格,它有我的第一個視圖控制器內的IBAction爲:Button sender.superview顯示爲UIViewController而不是我的CustomCell?

-(IBAction)AddButton:(UIButton *)sender{ 

ProductCell *cell = (ProductCell *)sender.superview; 
UITableView *tableView - (UITableView *)cell.superview; 
NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 

NSLog(@"Sender: %@, Super: %@", sender, sender.superview); 

//[saveitemArray addObject: [itemArray objectAtIndex:(indexPath.row + 1)]]; 
//[saveitemArray writeToFile:[self saveListPath] atomically:YES]; 
} 

逐步執行代碼顯示發件人爲一個UIButton但是變量「細胞'作爲FirstViewController。似乎只是完全跳過了牢房和桌子。我錯過了什麼簡單的事情?

更新:

好了,所以我向您展示全IBAction爲,註釋掉最後兩行我的手機變量顯示爲CustomCell(正確的上海華盈),但現在我的tableView變量顯示FirstViewController後superview而不是CustomTable?你的NSLog顯示相同,所以我改變它顯示單元格和cell.superview。下面是結果:

Sender: <UITableViewCellContentView: 0x716fb20; frame = (0 0; 320 43);  
gestureRecognizers = <NSArray: 0x71711d0>; layer = <CALayer: 0x716fbd0>>, Super: 
<ProductCell: 0x716f820; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize 
= W; layer = <CALayer: 0x716f990>> 

第二次更新:

它似乎認爲sender.superview是CellContentView而不是細胞本身,所以當我試圖讓indexPath它會嘗試使用cell作爲表格,CellContentView作爲單元格。我不認爲有必要瀏覽ContentView,我認爲.superview中的按鈕會直接轉到單元格本身?

回答:

好像你必須要經過這個順序的層次結構。

按鈕 - > CellContentView - >電池 - > TableView中

+0

請顯示一些'NSLog'。 'superview'不應該指向一個UIViewController子類。 –

+0

你可以嘗試'NSLog(@「發件人:%@,超級:%@」,sender,sender.superview);'?我想/希望調試器對你說謊。 :) –

+0

@PhillipMills好吧,所以我更新了顯示的IBAction,最後改爲NSlogging單元格變量。 – Jazzer008

回答

2

我想在接口建設者掛鉤的IBOutlets出事了。

設置在操作一個斷點,並嘗試遞歸登錄調試你的整個視圖以檢查它:

PO [自我視圖] recursiveDescription]。

更新

看起來你添加動作到細胞,而不是根據您的更新按鈕..

更新2:

- (IBAction) AddButton:(UIButton *)sender{ 

    UIView *cellContentView = (UIView *)sender.superview; 
    ProductCell *cell = (ProductCell *)cellContentView.superview; 
    UITableView *tableView - (UITableView *)cell.superview; 
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 

} 

或者你可以在這個方法中給按鈕一個標籤:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //your setup code here.. 

    cell.yourButton.tag = indexPath.row; 

} 

..去索引路徑在這裏,如果你只有1個部分:

- (IBAction) AddButton:(UIButton *)sender{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 
} 

應該這樣做!

+0

到目前爲止,一切看起來都很好。 TableView - > CustomCell - > CellContent View - > Button + Label。我應該特別尋找什麼? – Jazzer008

+0

不,我只是確認按鈕已連接到IBAction。 (摸起來) – Jazzer008

+0

仍然在線? http://chat.stackoverflow.com/rooms/15038/ios-developer-family – Tieme