【问题标题】:UITableViewCell is not clickableUITableViewCell 不可点击
【发布时间】:2012-10-26 07:47:44
【问题描述】:

我有一个自定义 UITableViewCell 有很多按钮、文本字段、标签和一个视图。我将它们隐藏并显示在每个索引路径上需要它们的位置。但由于某种原因/s 行不可点击!

我什至通过代码做了以下事情,以确保如果 IB 中出现问题,可以通过这些行修复它但没有用。

cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.userInteractionEnabled = YES;

我想知道隐藏视图是这里的罪魁祸首还是其他原因?

【问题讨论】:

  • iOS HIG:“应用程序响应手势,而不是点击” developer.apple.com/library/ios/documentation/userexperience/…
  • @phix23 - 是的,但它如何应用于我的案例?
  • 设置自定义对象 userInteractionEnabled 为否
  • 您可以在将所有内容添加到单元格的位置发布相关代码吗?了解您在何处以及如何操作每个单元将有助于调试问题。

标签: objective-c ios5 uitableview


【解决方案1】:

我有类似的问题。解决了,但添加了

self.clipsToBounds = YES;

到我的 tableView 所在的视图。所以我确定,我的 tableView 超出了其父视图的框架。

【讨论】:

    【解决方案2】:

    如果您的表格视图是可编辑的,这可能是单元格不可点击的原因

    如果是这种情况,并且您确实需要在编辑时可点击单元格 - 只需执行以下操作:

    tableView.allowsSelectionDuringEditing = YES;
    

    【讨论】:

    • 我可能听起来很愚蠢,但我可以在哪里查看 tableview 是否可编辑?
    • 默认情况下表格视图不可编辑。简单的查看方式是,当表格处于编辑模式时,您会在表格单元格旁边看到红色圆圈中的减号,或绿色圆圈中的加号。您可以将表格可编辑设置“编辑”属性设置为 TRUE,但您还应该实现一些额外的方法来定义编辑样式并处理编辑本身,您可以在此处阅读:developer.apple.com/library/ios/documentation/uikit/reference/…
    【解决方案3】:

    如果您真的不能正确触发“didSelectRowAtIndexPath”,请放置一个占据整个单元格的 BIG UIButton 并在按钮单击时执行必要的操作。只是一个想法! :-)

    【讨论】:

    • 谢谢。但这是不可能的,因为单元格中有文本字段。
    • 没问题,所有控件元素后面都可以有按钮,这样只有在没有其他元素响应的时候才会触发。
    【解决方案4】:

    我在我的 UITableViewCells 中也使用了很多不同的 UI 元素,为什么不尝试将 UITableViewCell 子类化并滚动你自己的类,并将项目作为单元格的属性?似乎对我有用。

    在 VincentsAwesomeCell.h 中:

    @interface VincentsAwesomeCell : UITableViewCell
    
    @property (weak, nonatomic) IBOutlet UILabel *FooLabel;
    @property (weak, nonatomic) IBOutlet UILabel *BarLabel;
    @property (weak, nonatomic) IBOutlet UIImageView *ShimmyIcon;
    @property (weak, nonatomic) IBOutlet UILabel *DooWopLabel;
    @property (weak, nonatomic) IBOutlet UIImageView *UseIcon;
    @property (weak, nonatomic) IBOutlet UILabel *TheForceLabel;
    @property (weak, nonatomic) IBOutlet UIImageView *bobSledIcon;
    @property (weak, nonatomic) IBOutlet UILabel *WristWatchLabel;
    @property (weak, nonatomic) IBOutlet UILabel *MeatLoafLabel;
    @property (weak, nonatomic) IBOutlet UILabel *SciFiLabel;
    @property (weak, nonatomic) IBOutlet UILabel *CollinderLabel;
    @property (weak, nonatomic) IBOutlet UILabel *RhubarbLabel;
    @property (weak, nonatomic) IBOutlet UITextField *SuperTextField;
    @property (weak, nonatomic) IBOutlet UIStepper *stepper;
    @property (strong, nonatomic) IBOutlet UILabel *amountOfAwesomeness;
    

    别忘了使用:

    在 VincentsAwesomeCell.m 中:

    - (void)prepareForReuse {
        [super prepareForReuse];
        // Always call this!
        // Reset custom stuff
    }
    

    重置单元格的属性以供重复使用。

    然后在你的 TableViewController.m 中:

    - (void) viewDidLoad
    {
        [super viewDidLoad];
        // Register the custom VincentsAwesomeCell NIB with the tableview
        UINib *nib = [UINib nibWithNibName:@"VincentsAwesomeCell" bundle:nil];
        [[self tableView] registerNib:nib forCellReuseIdentifier:@"VincentsAwesomeCell"];
    }
    

    至于

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    我将它用于 UIStepper 以跟踪按下哪个步进器,例如:

    [[cell stepper]setTag:indexPath.row];
        [[cell stepper]addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
    

    希望这会有所帮助!

    附言

    我使用 .xib 文件来创建自定义单元格的布局。只需将 UITableViewCell 拖入 InterFaceBuilder 并自定义即可!不过不要忘记链接属性!

    【讨论】:

      【解决方案5】:

      这是自定义UITableView的示例

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
      static NSString *CellIdentifier = @"Cell";
      
      UITableViewCell *cell    = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
      
          cell= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      
      }
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      
      UILabel *lbl = [[UILabel alloc]initWithFrame:cell.frame];
      lbl.backgroundColor = [UIColor clearColor];
      
      UIImageView *cellImage = [[UIImageView alloc]initWithImage:    [arrayImages objectAtIndex:indexPath.row]];
      if (pointRange.location!=NSNotFound) {
      
      
          cellImage.frame = CGRectMake(25,12.5, 75, 75);
          lbl.frame = CGRectMake(0, 0, tableView.frame.size.width, 100);
          lbl.font =  [UIFont fontWithName:@"PineappleDemo" size:30.0];
      }
      
      lbl.textColor =[UIColor colorWithRed:38.0/255.0 green:34.0/255.0 blue:98.0/255.0 alpha:1.0];
      lbl.text =[arrayData objectAtIndex:indexPath.row];
      
      
      
      
      for(UIView *v in [cell subviews])
      {
          if([v isKindOfClass:[UILabel class]]||[v isKindOfClass:[UIImageView class]]||[v isKindOfClass:[UIButton class]])
              [v removeFromSuperview];
      }
      if (index == indexPath.row) {
      
          UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 90, tableView.frame.size.width-200, 100)];
           lbl2.backgroundColor = [UIColor clearColor];
           lbl2.numberOfLines = 0;
          lbl2.text = [arrayOffers objectAtIndex:indexPath.row];
          [cell addSubview:lbl2];
          [lbl2 release];
      
          UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
          btn.frame = CGRectMake(tableView.frame.size.width-150, 100, 120, 84);
          btn.backgroundColor = [UIColor clearColor];
      
          if (indexPath.row<=2) {
              switch (indexPath.row ) {
                  case 0:
                      [btn setImage:[UIImage imageNamed:@"lickFB.png"] forState:UIControlStateNormal];
      
                      [btn addTarget:self action:@selector(facebook) forControlEvents:UIControlEventTouchUpInside];
                      break;
                  case 1:
                      [btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
      
                      [btn addTarget:self action:@selector(twitter) forControlEvents:UIControlEventTouchUpInside];
                      break;
                  case 2:
      
                      [btn setImage:[UIImage imageNamed:@"rateUs.png"] forState:UIControlStateNormal];
      
                      [btn addTarget:self action:@selector(rateAction) forControlEvents:UIControlEventTouchUpInside];
      
                      break;
      
                  default:
                      break;
              }
      
          }
          else{
              [btn setImage:[UIImage imageNamed:@"Buy_Button.png"] forState:UIControlStateNormal];
              switch (indexPath.row) {
                  case 3:
                      [btn addTarget:self action:@selector(deal1) forControlEvents:UIControlEventTouchUpInside];
                      break;
                  case 4:
                      [btn addTarget:self action:@selector(deal2) forControlEvents:UIControlEventTouchUpInside];
                      break;
                  case 5:
                      [btn addTarget:self action:@selector(deal3) forControlEvents:UIControlEventTouchUpInside];
                      break;
      
                  case 6:
                      [btn addTarget:self action:@selector(deal4) forControlEvents:UIControlEventTouchUpInside];
                      break;
      
      
      
                  default:
                      break;
              }
      
          }
          [cell addSubview:btn];
      
      }
      
      [cell addSubview:lbl];
      [cell addSubview:cellImage];
      [cellImage release];
      [lbl release];
      return cell;
      
      }
      

      【讨论】:

        【解决方案6】:

        在你自定义的 UITableViewCell 中,是否将 btn、textfields 等添加到 contentView 中?

        也许如果您将所有内容添加到 contentView,那应该可以解决您的问题。 希望对您有所帮助。

        【讨论】:

        • 请分享一些代码。你是如何创建自定义 UITableViewCell 的?
        【解决方案7】:

        试试这个tableView.allowsSelection=YES;

        你可以实现-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:

        【讨论】:

          【解决方案8】:

          实现这个方法:

          - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-05
            • 1970-01-01
            • 2016-02-13
            • 2021-10-11
            相关资源
            最近更新 更多