2012-03-12 79 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

相关问题