2012-03-12 98 views
4

使用iOS 5:::我有一個場景,我必須使用自定義單元格創建tableView。 自定義單元格有一個名爲TainingCellController的UITableViewCell的子類和一個NIB文件TrainingCell.xib。雖然父表放在一個UIViewController稱爲TrainingController內..IBAction在自定義UITableViewCell中的按鈕

現在我很認真地想知道,那CustomCell的文件所有者,誰收到IBActions或IBOutlets的關係..

在自定義單元格NIB文件,我可以更改文件所有者(默認設置爲NSObject),也可以單擊單元格本身並將其從UITableViewCell類更改爲TrainingCellContrller。

這兩個選項應該是什麼適當的類? IBActions &應該在哪裏定義IBOutlets(TrainingCellController或TrainingController)?

什麼如果我需要網點「自定義單元格中的標籤」在TrainingCellController中定義,而在TrainingController中定義按鈕動作?

回答

8

您會將您的UITableViewCell的班級設置爲您的CustomCell的班級,並且您將在CustomCell班級中定義IBoutlet s並連接它們。

,然後你將你的廈門國際銀行的文件所有者設置爲您ViewController,並在ViewController你將宣佈一個

IBOutlet CustomCell *yourClassLevelCell; 

IBOutlet連接到您的廈門國際銀行的UITableViewCell

現在

時,你會用來初始化的你的ViewController's方法cellForRowAtIndexPath你將手動添加目標的細胞,如下所示:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = yourClassLevelCell; 
    [cell.button addTarget:self ... ]; 
    //button is IBOutlet in your CustomCell class which you will have 
    //connected to your Button in xib 
} 
+1

大工作了......所有的雜波清除.. !! =)謝謝你Adil .. 順便說一下,直接連接從自定義單元的按鈕IBAction文件的所有者(UIViewController)也工作..! – tGilani 2012-03-12 13:49:35

+1

好吧,在這種情況下,你不能在另一個'UIViewController'中使用這個UITableViewCell,你將不得不再次通過代碼添加目標。 :) – 2012-10-19 05:30:55

+0

點採取.. !! – tGilani 2012-10-19 07:33:59

1

嘗試用動態按鈕在同一類的tableView

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkRequestedCC" owner:self options:nil]; 
{ 
     for (id oneObject in nib) if ([oneObject isKindOfClass:[WorkRequestedCC class]]) 
      cell = (WorkRequestedCC *)oneObject; 


    } 

    UILabel *Button=[[UIBUtton alloc]initWithFrame:CGRectMake(792, 13, 10, 15)]; 

    [Button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:Button]; 
} 

-(void) ButtonClicked 
{ 
    //your code here 
    } 
} 
+0

謝謝您的輸入.. !! – tGilani 2012-03-13 09:55:54

+0

你的代碼示例包含兩個散開的大括號(第6行和倒數第二行)。請更正此代碼以說清楚。 – 2012-07-27 11:13:38