在表格视图单元格内使用按钮打开URL [英] Open URL with a button inside a table view cell

查看:69
本文介绍了在表格视图单元格内使用按钮打开URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个表格单元格中包含一个打开URL的按钮.

I want to include a button in each table cell that opens a URL.

我已经创建了带有图像和标签的表(使用数组),但是我对如何创建按钮感到困惑

I've created tables (using an array) with images and labels just fine, however I'm confused how to create a button

这是我到目前为止所拥有的

Here's what I have so far

class ExploreCell: UITableViewCell {

@IBOutlet weak var exploreImageView: UIImageView!
@IBOutlet weak var exploreTitleView: UILabel!
@IBOutlet weak var exploreDescriptionView: UILabel!
@IBOutlet weak var exploreButton: UIButton!

func setExplore(explore: Explore) {
    exploreImageView.image = explore.image
    exploreTitleView.text = explore.title
    exploreDescriptionView.text = explore.description
    exploreButton.addTarget(self, action: "connected:", for: .touchUpInside) = explore.button

}

我的数组的类如下

class ExploreListScreen: UIViewController {

@IBOutlet weak var tableView: UITableView!

var explores: [Explore] = []

override func viewDidLoad() {
    super.viewDidLoad()
    explores = createArray ()

    tableView.delegate = self
    tableView.dataSource = self
}

func createArray() -> [Explore] {

    var tempExplores: [Explore] = []

    let explore1 = Explore(image: #imageLiteral(resourceName: "test"), title: "Demo", description: "Essential", button: "")

    tempExplores.append(explore1)

    return tempExplores
}

最后我还有另一个文件,其中包含声明的变量

Finally I have another file which contains the declared variables

class Explore {

var image: UIImage
var title: String
var description: String
var button: UIButton

init(image: UIImage, title: String, description: String, button: UIButton) {
    self.image = image
    self.title = title
    self.description = description
    self.button = button
}

任何建议和指导都是很棒的.谢谢!

Any advice and guidance would be fantastic. Thank-you!

推荐答案

这是我通常解决此问题的方式.为您的UITableViewCell子类创建一个委托,并将拥有tableView的视图控制器设置为其委托.添加用于在单元格内部进行交互的方法.

Here's how I usually solve this. Create a delegate for your UITableViewCell subclass, and set the view controller owning the tableView as its delegate. Add methods for the interactions that happens inside the cell.

protocol YourTableViewCellDelegate: class {
    func customCellDidPressUrlButton(_ yourTableCell: YourTableViewCell)
}

class YourTableViewCell: UITableViewCell {
    weak var delegate: YourTableViewCellDelegate?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let button = UIButton()
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        addSubview(button)
    }

    required init?(coder _: NSCoder) {
        return nil
    }

    @objc func buttonTapped() {
        delegate?.customCellDidPressUrlButton(self)
    }

}

然后,在控制器中,将其自身设置为委托,并通过适当的方法indexPath(for:)

Then, in the controller, set itself as a delegate and get the indexPath trough the proper method, indexPath(for:)

class YourTableViewController: UITableViewController {

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourTableViewCell
        cell.delegate = self
        return cell
    }
}

extension YourTableViewController: YourTableViewCellDelegate {
    func customCellDidPressUrlButton(_ yourTableCell: YourTableViewCell) {
        guard let indexPath = tableView.indexPath(for: yourTableCell) else { return }
        print("Link button pressed at \(indexPath)")
    }
}

然后使用该indexPath来获取正确的URL,并使用SFSafariViewController从您的表viewcontroller中显示它.

Then use that indexPath to grab the correct URL and present it from your table viewcontroller with a SFSafariViewController.

这篇关于在表格视图单元格内使用按钮打开URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆