【问题标题】:Assign UIButton event dynamically in UITableViewCell在 UITableViewCell 中动态分配 UIButton 事件
【发布时间】:2012-06-12 06:51:27
【问题描述】:

我创建了一个自定义 UITableViewCell 子类,其中包含一个名为 testbtn 的按钮。然后我尝试将该按钮的事件分配给另一个类,但这对我不起作用。请问你能告诉我哪里出错了吗?

这是我的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"PatientCell";
    PatientCell *cell = (PatientCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:@"PatientCell" owner:self options:nil];
            for (id currentlevelob in TopLevelOb) {
                if([currentlevelob isKindOfClass:[PatientCell class]]){
                    cell = (PatientCell *) currentlevelob;
                    cell.test.text = @"some text";
                    cell.HR.text =@"80";
                    cell.ABP.text =@"120/20";
                    cell.SOP2.text =@"95";
                    cell.RR.text=@"98";
                    [cell.testbtn addTarget:self action:@selector(openLiveData:)   
                       forControlEvents:UIControlEventTouchUpInside];// **this event is not fire to openLiveData method**
                    break;
                }
            }
        } else {
            NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:@"PatientCell_iPad" owner:self options:nil];
            for (id currentlevelob in TopLevelOb) {
                if([currentlevelob isKindOfClass:[PatientCell class]]){
                    cell = (PatientCell *) currentlevelob;
                    cell.test.text = [nameArry objectAtIndex:indexPath.row];
                    cell.HR.text =@"80";
                    cell.ABP.text =@"120/20";
                    cell.SOP2.text =@"95";
                    cell.RR.text=@"98";
                    [cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside]; 
                    break;
                }
            }     
        }
    }
    return cell;
}

-(IBAction)openLiveData:(id)sender{
     NSLog(@"hello ");
 }

【问题讨论】:

  • 所以你所有的单元格都被绘制了,但是你不能按下按钮?如果你按下它,它不会打印到控制台?
  • 检查按钮是否连接到您的NIB中的testBtn实例变量。
  • @AdrianAncuta 是的。它没有调用打印到控制台的那个数学模型
  • @timthetoolman 哦!!对不起,我忘记将实例变量链接到我的 XIB
  • 你现在可以工作了,谢谢大家

标签: iphone objective-c ios uitableview ios5


【解决方案1】:

检查您的按钮是否已连接到您的 NIB 中的 testBtn 实例变量。

祝你好运

T

【讨论】:

    【解决方案2】:

    这样做是因为忘记指定控制事件:

      [cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside];
    

    希望有帮助

    【讨论】:

      【解决方案3】:

      它在 iPhone 版本中应该可以正常工作,但在 iPad 版本中应该是一个问题,正如您提到的 forControlEvents:UIControlStateNormal 代替 forControlEvents:UIControlEventTouchUpInside

      【讨论】:

      • iPhone版和iPad版应该也不能正常使用
      • 我看到你已经更新了代码。如果您正在尝试编写按钮的选择器,为什么不在自定义单元格类中编写。也可以通过 Nib 文件连接它,这样可能会为您省去很多麻烦。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多