【问题标题】:How to get the indexpath.row when a UIImageView in a cell is tapped?点击单元格中的 UIImageView 时如何获取 indexpath.row?
【发布时间】:2016-02-19 07:54:28
【问题描述】:

swift: how to get the indexpath.row when a button in a cell is tapped?

此链接是点击按钮时的答案,如果我的问题是不可能的,我将只使用一个按钮并将图像放在上面。我只是想知道是否可以通过点击UIImageView 而不是按钮来实现这一点。我用UIImageView 而不是UIButton 尝试了确切的答案,我得到了这个错误

“致命错误:在展开可选值时意外发现 nil”。

cell.imagePostedUIImageView

let tapGR = UITapGestureRecognizer(target: self,action:Selector("imageTapped:"))
cell.imagePosted.userInteractionEnabled = true
cell.imagePosted.addGestureRecognizer(tapGR);

func imageTapped(img: AnyObject)
{
    if let imgView = img as? UIImageView {

        if let superView = imgView.superview {

            if let cell = superView.superview as? CellCustom {

                indexPath2 = self.tableView.indexPathForCell(cell)

            }      
       }       
   }

   print(indexPath2.row)       
}

【问题讨论】:

    标签: ios swift uitableview uiimageview


    【解决方案1】:

    这可能对你有帮助,

    UITapGestureRecognizer 添加到UIImageView 您可以将indexpath.row 存储在UIImageView 的标签属性中,并在UITapGestureRecognizer 事件中访问该标签

    例如(Objective-C):

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
        tap.cancelsTouchesInView = YES;
        tap.numberOfTapsRequired = 1;
        [cell.imageView addGestureRecognizer:tap];
    
        cell.imageView.tag = indexPath.row;
    

    并获得indexpath.row

    -(void)handleImageTap:(UITapGestureRecognizer *)gestureRecognizer{
        UIView* view = gestureRecognizer.view;
        CGPoint loc = [gestureRecognizer locationInView:view];
        NSInteger indexpath = [view hitTest:loc withEvent:nil].tag;
        NSLog(@"%ld",(long)indexpath);
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用imageViewInCellTapped(cell:YourCellType) 之类的方法和单元格中的委托属性创建协议。在cellForRowAtIndexPath 中,将控制器设置为每个单元的委托,并实现协议中的方法。当您的单元格中发生某些事情(例如点击按钮或图像)时,您可以调用 delegate?.imageViewInCellTapped(self) 其中 self 是单元格,然后在实现此方法的控制器中使用 tableView 的 indexPathForCell 方法获取索引。

      【讨论】:

      • 感谢 Lucian 的回答,但 Madan 的回答对我有用,而且我是初学者,还不熟悉协议。我在 Udemy 上做的教程由于某种原因没有提到协议。
      • 您需要注意单元格的重用,如果单元格被重用,您最终可能会得到错误的 indexPath 值。
      • 你介意给我一个单元重用的例子吗?所以我知道不要这样做。
      • 单元格在滚动时会自动重复使用,您可以在单元格中使用 prepareForReuse 来重置其状态。
      【解决方案3】:

      如果有人感兴趣,这里是 Madan 的 swift 代码:

          func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
      
          var view: UIView!
          var loc: CGPoint!
          view = gestureRecognizer.view
          loc = gestureRecognizer.locationInView(view)
      
          var indexPath: NSInteger!
      
          indexPath = (view.hitTest(loc, withEvent: nil)?.tag)!
      
          print(indexPath)
      
      }
      

      【讨论】:

        【解决方案4】:

        希望此代码有助于获取轻敲单元格 swift 3 的索引路径:

          func nameTapped(_ sender:UITapGestureRecognizer)
        {
            var pointVAlue = CGPoint()
            pointVAlue = sender.location(in: infoTable)
        
            var indexPath = IndexPath()
            indexPath = infoTable.indexPathForRow(at: pointVAlue)!
        
            print(indexPath.row)
        
           /* if indexPath != nil {
        
                let userEnt  = ratingArray.object(at: indexPath.row)as! RateEntity
        
                let detailVc = AssociateDetailViewController()          
        
                if ((userEnt.userId as String).isEmpty == false )
                {
                    detailVc.m_otherUserId = userEnt.userId as String
                    self.navController!.pushViewController(detailVc, animated: true)
                }
            }*/
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-23
          相关资源
          最近更新 更多