【问题标题】:How to disable tableViewCell selection for a particular part in the UITableView cell iOS [duplicate]如何在 UITableViewcell iOS 中禁用特定部分的 uitableViewCell 选择 [重复]
【发布时间】:2017-04-07 00:47:36
【问题描述】:

我是 iOS 新手,现在我需要执行与 UITableView 单元格相关的两个操作,我的单元格中有一个检查图像,一旦我点击它,我就会得到一个触摸响应,但是如果我点击任何地方除了图像区域之外,didSelectRowAtIndexPath 方法正在被调用。

我应该怎么做才能只为单元格内的选定区域调用didSelectRowAtIndexPath 而忽略剩余区域,或者禁用对单元格少数区域的触摸。

My layout looks like this

【问题讨论】:

  • 可以给你的tableviewcell截图吗?
  • 禁用触摸的区域是否与您参考的图像分开?所以单元格有 3 个不同的区域:1)触发 didSelectRowAtIndexPath,2)检查你的图像,3)不响应触摸?
  • @dylansturg:是的..!不是三个,而是有两个区域,1. Till check image 和 2. check image 区域。问题是,如果我只是在图像 didSelectRowAtIndexPath 方法调用的外部轻轻点击,我需要将其设为可选。
  • 可以在单元格的特定区域添加点击手势,不需要实现didselectrowatindexpath方法。
  • 我正在使用自定义类来识别图像点击并获得正确的响应。但正如我上面所说,如果用户在图像边界之外轻轻点击,则会触发 didSelectRowAtIndexPath 方法,因此我需要使该区域不可触摸。

标签: ios objective-c iphone swift uitableview


【解决方案1】:

我了解您的问题。您必须在自定义 UITableViewCell 中覆盖 touchesBegan(touches: Set, withEvent event: UIEvent?) 方法。

 class CustomTableViewCell: UITableViewCell {
   var isValidTouch: Bool = true

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first {
       let point = touch.locationInView(self.contentView)
       let validTouchPoint = self.contentView.bounds.width - (checkMarkView.bounds.width + 16.0) //Trailing + Leading space
        if point.x < validTouchPoint {
            isValidTouch = true
        } else {
            isValidTouch = false
            //Send tap gesture callback to your viewcontroller
        }
    }
    super.touchesBegan(touches, withEvent: event)
  }

}

在您的视图控制器 UITableViewDelegate 方法中,只需检查您的触摸是否有效。

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
    if cell.isValidTouch {
      //do whatever
    }
}

【讨论】:

    【解决方案2】:

    请在cellForRowAtIndexPath中写下这段代码:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    如果您添加复选框并取消选中复选框,则将此代码写入 cellForRowAtIndexPath

    if(indexPath.row == 0){
    UIButton *btnForCheckAC = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.size.width - 50 , 10,15,15)];
    btnForCheckAC.tag = BUSTYPEBTN_TAG;
    UIImage *image = (isAcButtonClkd)?[UIImage imageNamed:@"check-mark-pink.png"]:[UIImage imageNamed:@"uncheckbox.png"];
    
    [btnForCheckAC addTarget:self action:@selector(busTypebtnAction:event:) forControlEvents:UIControlEventTouchUpInside];
    [btnForCheckAC setImage:image forState:UIControlStateNormal];
                    cell.accessoryView = btnForCheckAC;
    }
    

    然后在didSelectedRowAtIndexPath

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
    
           isAcButtonClkd = !isAcButtonClkd;
    
        }
    }
    

    编写按钮动作:

    -(void)busTypebtnAction:(id *)sender event:(id)event{
    
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:self.tblBookBus];
        NSIndexPath *indexPath = [self.tblBookBus indexPathForRowAtPoint: currentTouchPosition];
        if (indexPath != nil)
        {
            [self tableView: self.tblBookBus didSelectRowAtIndexPath: indexPath];
        }
    
    }
    

    根据您的要求更改名称。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:
      1. 设置tableview选择样式为UITableViewCellSelectionStyleNone

      2. 实现以下任何委托方法,这会阻止 didSelectRowAtIndexPath 被调用。

      3. 为你的检查ImageView设置userInteractionEnabled并实现图像点击的方法(使用UIButton或UIImageView与TapGesture)

        - (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
               return nil;    
        }
        

        或

        - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
             return NO;
        }
        

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        首先将该 imageview 设为 uibutton

        并将cellselectionstyle 设为无。

        在cellForRowIndexPath中:

        {
        [cell.CloseButton addTarget:self action:@selector(CrossClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.CloseButton setTag:indexPath.row];
        
        }
        

        in 方法获取点击按钮行号的标签值 像这样:

        -(void)CrossClicked:(UIButton *)sender{
        
        int index = sender.tag;
        
        //on basis of index you can perform all task
        
        }
        

        【讨论】:

          【解决方案5】:

          如果您的检查图像是 UIBUtton,那么试试这个

          override func viewDidLoad() {
              super.viewDidLoad()
              tableView.allowsSelection = false;
          }
          

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-04-05
            • 2021-10-31
            • 2011-06-08
            • 1970-01-01
            • 2017-12-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多