繁体   English   中英

在Custom UITableViewCell中的按钮上进行IBAction

[英]IBAction on a button in Custom UITableViewCell

使用iOS 5 ::我有一个场景,我必须使用自定义单元格创建一个tableView。 自定义单元格有一个名为TainingCellController的UITableViewCell子类和一个NIB文件TrainingCell.xib。 而父表放在一个名为TrainingController的UIViewController中。

现在我真的很想知道,CustomCell与文件所有者的关系,谁接收IBActions或IBOutlets ..

在Custom Cell NIB文件中,我可以更改文件所有者(默认设置为NSObject),也可以单击单元格本身并将其类从UITableViewCell更改为TrainingCellContrller。

这两个选项的适当类应该是什么? 应该在哪里定义IBActions和IBOutlets(TrainingCellController或TrainingController)?

什么如果我需要在TrainingCellController中定义按钮动作时在TrainingCellController中定义“自定义单元格中的标签”的插座?

您将UITableViewCell的类设置为CustomCell的类,您将在CustomCell类中定义IBoutlet并连接它们。

然后,您将Xib的文件所有者设置为ViewController ,在ViewController您将声明一个

IBOutlet CustomCell *yourClassLevelCell;

并将此IBOutlet连接到Xib的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
}

尝试在同一个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