简体   繁体   English

在Custom UITableViewCell中的按钮上进行IBAction

[英]IBAction on a button in Custom UITableViewCell

Using iOS 5 : : : I have a scenario where I must create a tableView with Custom Cells. 使用iOS 5 ::我有一个场景,我必须使用自定义单元格创建一个tableView。 The custom cells have a Controller called TainingCellController Subclass of UITableViewCell and a NIB file TrainingCell.xib . 自定义单元格有一个名为TainingCellController的UITableViewCell子类和一个NIB文件TrainingCell.xib。 While the parent table is placed inside a UIViewController called TrainingController.. 而父表放在一个名为TrainingController的UIViewController中。

Now I am seriously wondering, the relationship of that CustomCell to the File Owner, who receives the IBActions or IBOutlets.. 现在我真的很想知道,CustomCell与文件所有者的关系,谁接收IBActions或IBOutlets ..

In the Custom Cell NIB file, I can change the file owner (by default set to NSObject) and also can click on the cell itself and change it's class from UITableViewCell to TrainingCellContrller .. 在Custom Cell NIB文件中,我可以更改文件所有者(默认设置为NSObject),也可以单击单元格本身并将其类从UITableViewCell更改为TrainingCellContrller。

What should be the appropriate classes for these two options ?? 这两个选项的适当类应该是什么? Where should the IBActions & IBOutlets be defined (TrainingCellController or TrainingController)? 应该在哪里定义IBActions和IBOutlets(TrainingCellController或TrainingController)?

And what If I need outlets to "labels in custom cell" to be defined in TrainingCellController while button action to be defined in TrainingController?? 什么如果我需要在TrainingCellController中定义按钮动作时在TrainingCellController中定义“自定义单元格中的标签”的插座?

You will set your UITableViewCell 's class to your CustomCell 's class and you will defined IBoutlet s in CustomCell class and connect them. 您将UITableViewCell的类设置为CustomCell的类,您将在CustomCell类中定义IBoutlet并连接它们。

And then you will set your Xib's file owner to your ViewController , and in your ViewController you will declare an 然后,您将Xib的文件所有者设置为ViewController ,在ViewController您将声明一个

IBOutlet CustomCell *yourClassLevelCell;

and connect this IBOutlet to your Xib's UITableViewCell 并将此IBOutlet连接到Xib的UITableViewCell

now when you will initilize the cell inside your ViewController's method cellForRowAtIndexPath you will add target manually, something like this: 现在,当您在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
}

Try working out with dynamic buttons on the same tableView class 尝试在同一个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
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM