简体   繁体   English

如何获取UIButton在Tableview单元格中的位置,以及如何在Swift中添加目标?

[英]How to get location of UIButton in Tableview cell, and add target in Swift?

I want to add a button on TableviewCell in Swift to popup a small custom view (cover current view) with some info from online. 我想在Swift的TableviewCell上添加一个按钮,以弹出一个小的自定义视图(覆盖当前视图),其中包含一些在线信息。

I created another (ViewController + Custom View for the 'popup view' and resized it. 我创建了另一个(“弹出式视图”的ViewController +自定义视图,并调整了它的大小。

In the tableviewcell class, in the Button.AddTarget(..) related function, I have called PresentViewController(..) . 在tableviewcell类中,在Button.AddTarget(..)相关函数中,我调用了PresentViewController(..)

I have these problems: 我有这些问题:

  1. the popup view appears in the top of screen because I set fixed X,Y Gfloat for it. 弹出视图出现在屏幕顶部,因为我为其设置了固定的X,Y Gfloat。 I want the popup to appear just below each button. 我希望弹出窗口显示在每个按钮的正下方。

How do I get the X,Y coordinate of the button (or touched location) in screen window in the TableviewCell ? 如何在TableviewCell的屏幕窗口中获取按钮(或触摸位置)的X,Y坐标

  1. In the TableviewCell , when I click the button (not the cell), how can I push a specific value (id_item) to request data? TableviewCell中 ,当我单击按钮 (而不是单元格)时,如何推送特定值(id_item)以请求数据?

I searched but most related answers were about TableviewController not TableviewCell. 我进行了搜索,但最相关的答案是关于TableviewController而不是TableviewCell。

======================================================== ================================================== ======

I have tried: 我努力了:

import UIKit
import QorumLogs
class TabelViewSearchStockCell: UITableViewCell, UIViewControllerTransitioningDelegate {

    @IBOutlet weak var mainImageView: UIImageView!
    @IBOutlet weak var productCodeLabel: UILabel!

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var stockButton: UIButton!

    @IBOutlet weak var view: UIView!

        var cellType:String?
        var searchCell: SearchResult?
            {
            didSet{
                // 1. set image
                if let imageId = searchCell?.id_img{
                    let urlCovertor = GetImgUrl()
                    let urlStr = urlCovertor.getSmall(imageId)
                    let url = NSURL(string: urlStr)
                    mainImageView.sd_setImageWithURL(url)
                }
                // 2. set_title
                titleLabel.text = searchCell?.title
                // 3. set product code
                productCodeLabel.text = searchCell?.productcode
                // 4. set price area
                stockButton.addTarget(self, action: #selector(TabelViewSearchStockCell.checkStock), forControlEvents: .TouchUpInside)

            }
        }

    // MARK: - set location
    private lazy var animatorManager: PresentationManager = {
        let buttonRect = self.stockButton.superview?.convertRect(self.stockButton.frame, toView: self.view)
        let manager = PresentationManager()
        manager.presentFrame = buttonRect!
        return manager
    }()


}

// MARK:- Listen button
extension TabelViewSearchStockCell{
    @objc private func checkStock(){
        // create menu
        let sb = UIStoryboard(name: "Popover",bundle: nil)
        guard let menuView = sb.instantiateInitialViewController() else{
            return
        }
        menuView.transitioningDelegate = animatorManager
        menuView.modalPresentationStyle = UIModalPresentationStyle.Custom
        self.window?.rootViewController?.presentViewController(menuView, animated: true, completion: nil)

    }
}

It still always appeared in C1 它仍然总是出现在C1中

在此处输入图片说明

I want when I click button in location S1, it will show 'popup' in location C2 not C1. 我想要单击位置S1上的按钮时,它将在位置C2而不是C1上显示“弹出窗口”。 That means, the popupview will always show just below the button I clicked. 这意味着,popupview将始终显示在我单击的按钮的正下方。

For your first question, you can call convertRect on your button's super view, which should be the table view cell's content view. 对于第一个问题,您可以在按钮的超级视图上调用convertRect ,超级视图应该是表格视图单元格的内容视图。 If you can't read the code given by harpreet, here's the swift translation: 如果您无法阅读harpreet给出的代码,则以下为快速翻译:

let frame = yourButton.superview.convertRect(yourButton.frame, toView: self.view)

Now this frame is the frame of the button relative to self.view . 现在,此框架是按钮相对于self.view的框架。

For your second question, you can set the tag property of your button and call viewWithTag on the table view cell's content view to get the button back. 对于第二个问题,您可以设置按钮的tag属性,并在表视图单元格的内容视图上调用viewWithTag来取回按钮。

Here's how: 这是如何做:

If your cell is in the storyboard, select the button, and set the "Tag" to some number in the property inspector on the right. 如果您的单元格位于情节提要中,请选择按钮,然后在右侧的属性检查器中将“标签”设置为某个数字。 If you create the button by code, you can set the tag by 如果您通过代码创建按钮,则可以通过以下方式设置标签

yourButton.tag = someNumber

Then you just need to get the table view cell's contentView and call viewWithTag : 然后,您只需要获取表视图单元格的contentView并调用viewWithTag

let contentView = cell.contentView
let button = contentView.viewWithTag(someNumber)!

Now you can add a target! 现在您可以添加目标了!

button.addTarget(...)

EDIT: 编辑:

If you're using a storyboard, you can control-drag the button to your code, and create an Action. 如果您使用的是情节提要,则可以控制将按钮拖到代码中,然后创建一个动作。 Be careful! 小心! Don't accidentally add an outlet instead! 不要意外添加插座! Remember to change "Outlet" to "Action" first in the pop up! 请记住,首先在弹出窗口中将“插座”更改为“动作”!

what you have to do is to convert a CGRect of button in UITableViewCell in UITableView to CGRect with respect to self.view. 您要做的是相对于self.view将UITableView中UITableViewCell中的按钮的CGRect转换为CGRect。

CGRect frame = [[youButton superview] convertRect: yourButton.frame toView: self.view];

you can use this frame's origin to play with the positioning of your popup view 您可以使用此框架的原点来播放弹出视图的位置

What is your meaning about "how can I push a specific value (id_item) out?" 您对“我如何推出特定值(id_item)”有什么含义? in your 2nd problem? 在你的2nd问题?

I suggest that you add your button target out of the cell (IE: use button.add target... when you implement the tableView's dataSource function cell for row in the controller). 我建议您将按钮目标添加到单元格之外(即:在控制器中cell for row实现tableView的dataSource函数cell for row时,请使用button.add target... )。

Then you can do what you want do as usual, good luck. 然后,您可以照常做自己想做的事,祝您好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM