发送tableviewcell的索引路径

时间:2017-06-25 03:18:01

标签: ios swift popup

我在tableViewCell中有一个图像,当我点击它时,我会看到一个弹出窗口(另一个UIView)。当我点击tableviewcell上的图像时,我希望将图像传递给弹出窗口。这是我设置弹出窗口的方法。

  @IBAction func PopUpClicked(_ sender: Any) -> Void {
            self.transparentBackground = UIView(frame: UIScreen.main.bounds)
            self.transparentBackground.backgroundColor = UIColor(white: 0.0, alpha: 0.8)
            UIApplication.shared.keyWindow!.addSubview(self.transparentBackground)
            self.opaqueView = self.setupOpaqueView()
            self.transparentBackground.addSubview(opaqueView)
            UIApplication.shared.keyWindow!.bringSubview(toFront: self.transparentBackground)
            self.view.bringSubview(toFront: transparentBackground)

    }

 func setupOpaqueView() -> UIView{


        let mainView = UIView(frame: CGRect(x: 16, y: 132, width: Int(UIScreen.main.bounds.width-32), height: 403))
        mainView.backgroundColor = UIColor(red:0.23, green:0.73, blue:1.00, alpha:1.0)
        mainView.layer.cornerRadius = 6


        let  OKbutton = UIButton(frame: CGRect(x: 151, y: Int(mainView.frame.height-60), width: 40, height: 40))
        OKbutton.setImage( UIImage.init(named: "AddOn"), for: .normal)
        OKbutton.layer.borderColor = UIColor.white.cgColor
        OKbutton.layer.borderWidth = 2
        OKbutton.layer.cornerRadius = 20



        self.imageView = UIImageView(frame: CGRect(x: 29, y: 18, width: 274, height: 310))
        self.imageView.layer.backgroundColor = UIColor.white.cgColor


        mainView.addSubview(OKbutton)
        mainView.addSubview(self.imageView)
        OKbutton.addTarget(self, action:  #selector(ThirdWheelViewController.handleOKButtonTapped(_:)), for: .touchUpInside)

        return mainView

    }

    func handleOKButtonTapped(_ sender: UIButton){
        removeAnimate()

    }

    func removeAnimate()
    {
        UIView.animate(withDuration: 0.25, animations: {
            self.transparentBackground.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
            self.transparentBackground.alpha = 0.0;

        }, completion:{(finished : Bool)  in
            if (finished)
            {
                self.transparentBackground.removeFromSuperview()
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

在cellForItemAtIndexPath中

将目标添加到您的项目并为其标记

在商品标签

中发送索引

像这样的事情

 let cell: CcImageGallery = collectionView.dequeueReusableCellWithReuseIdentifier("CcImageGallery", forIndexPath: indexPath) as! CcImageGallery


    cell.btnShape.setImage(UIImage(named: shapeImg[indexPath.row] as! String), forState: .Normal)
    cell.btnShapeText.setTitle(shapeTxt[indexPath.row] as? String, forState: .Normal)

    cell.btnShapeRadio.addTarget(self, action:  #selector(selectImage(_:)), forControlEvents: .TouchUpInside)
    cell.btnShapeRadio.tag = indexPath.row

    cell.btnShape.addTarget(self, action:  #selector(selectImage(_:)), forControlEvents: .TouchUpInside)
    cell.btnShape.tag = indexPath.row

    cell.btnShapeText.addTarget(self, action:  #selector(selectImage(_:)), forControlEvents: .TouchUpInside)
    cell.btnShapeText.tag = indexPath.row


    return cell

答案 1 :(得分:0)

cellForRow功能中,以这种方式添加图片点击监听器

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)

然后,获取以这种方式点击的图像

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let tappedImage = tapGestureRecognizer.view as! UIImageView

    // Your action
}

最后,在用户点击另一个按钮之前,您不需要此图像。所以你需要的不是传递图像。您只需将图像存储在内存中,然后单击从中读取按钮即可。所以你以这种方式定义imageTapped函数

var tappedImage: UIImage?
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    tappedImage = tapGestureRecognizer.view as! UIImageView
}

然后,当用户点击使用PopUpClicked创建UIView的{​​{1}}函数时,请在self.opaqueView = self.setupOpaqueView()函数中读取此存储的图像变量

setupOpaqueView()