简体   繁体   中英

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.

I created another (ViewController + Custom View for the 'popup view' and resized it.

In the tableviewcell class, in the Button.AddTarget(..) related function, I have called PresentViewController(..) .

I have these problems:

  1. the popup view appears in the top of screen because I set fixed X,Y Gfloat for it. 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 ?

  1. In the TableviewCell , when I click the button (not the cell), how can I push a specific value (id_item) to request data?

I searched but most related answers were about TableviewController not 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

在此处输入图片说明

I want when I click button in location S1, it will show 'popup' in location C2 not C1. That means, the popupview will always show just below the button I clicked.

For your first question, you can call convertRect on your button's super view, which should be the table view cell's content view. If you can't read the code given by harpreet, here's the swift translation:

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

Now this frame is the frame of the button relative to 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.

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 :

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.

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?" in your 2nd problem?

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).

Then you can do what you want do as usual, good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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