Ios 使用表视图单元格内的按钮打开URL

Ios 使用表视图单元格内的按钮打开URL,ios,swift,Ios,Swift,我想在每个表格单元格中包含一个打开URL的按钮 我已经创建了带有图像和标签的表(使用数组),但是我不知道如何创建按钮 这是我到目前为止所拥有的 class ExploreCell: UITableViewCell { @IBOutlet weak var exploreImageView: UIImageView! @IBOutlet weak var exploreTitleView: UILabel! @IBOutlet weak var exploreDescriptionView: U

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

我已经创建了带有图像和标签的表(使用数组),但是我不知道如何创建按钮

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

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
}
最后,我有另一个文件,其中包含声明的变量

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
}

任何建议和指导都会非常棒。谢谢大家!

Swift 4

这是使用
touchPoint

class YourTableViewController: UITableViewController {

    // ...

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SwiftyCell", for: indexPath) as! SwiftyTableViewCell

        cell.label.text = "This is cell number \(indexPath.row)"

        // WRONG! When cells get reused, these actions will get added again! That's not what we want.
        // Of course, we could get around this by jumping through some hoops, but maybe there's a better solution...
        cell.yourButton.addTarget(self, action: #selector(self.yourButtonTapped(_:)), for: .touchUpInside)

        return cell
    }

    func yourButtonTapped(_ sender: Any?) {
        let point = tableView.convert(sender.center, from: sender.superview!)

        if let wantedIndexPath = tableView.indexPathForItem(at: point) {
            let cell = tableView.cellForItem(at: wantedIndexPath) as! SwiftyCell

        }
    }
    // ...

}

Swift 4

这是使用
touchPoint

class YourTableViewController: UITableViewController {

    // ...

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SwiftyCell", for: indexPath) as! SwiftyTableViewCell

        cell.label.text = "This is cell number \(indexPath.row)"

        // WRONG! When cells get reused, these actions will get added again! That's not what we want.
        // Of course, we could get around this by jumping through some hoops, but maybe there's a better solution...
        cell.yourButton.addTarget(self, action: #selector(self.yourButtonTapped(_:)), for: .touchUpInside)

        return cell
    }

    func yourButtonTapped(_ sender: Any?) {
        let point = tableView.convert(sender.center, from: sender.superview!)

        if let wantedIndexPath = tableView.indexPathForItem(at: point) {
            let cell = tableView.cellForItem(at: wantedIndexPath) as! SwiftyCell

        }
    }
    // ...

}

只需在
viewDidLoad
中创建
UIButton
对象,并将此按钮作为
cellforrowatinexpath
函数中单元格的子视图添加。根据您的要求使用Burton的框架。

只需在
viewDidLoad
中创建
UIButton
对象,并将此按钮添加为
cellForRowAtIndexPath
函数中单元格的子视图。根据您的要求使用伯顿镜框。

以下是我通常的解决方法。为
UITableViewCell
子类创建委托,并将拥有tableView的视图控制器设置为其委托。为单元内部发生的交互添加方法

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

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


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

我通常是这样解决这个问题的。为
UITableViewCell
子类创建委托,并将拥有tableView的视图控制器设置为其委托。为单元内部发生的交互添加方法

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

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


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

您想在按钮单击中设计按钮或导航不同的url我想导航到“createArray”函数中指定的url。继续此url了解自定义单元格:)您想在按钮单击中设计按钮或导航不同的url我想导航到“createArray”函数中指定的url函数。继续此URL了解自定义单元格:)这是一种方法,但不是最好的方法。最好的方法是什么,请与我们分享。我真的很想了解更多详细信息。谢谢,请看我的答案。是的,那很好,但这怎么可能是最好的方法呢?请解释一下,如果我能学到一些新的东西,那将是非常棒的。这是一种方式,但不是最好的方式。最好的方式是什么?请分享一下,我真的很想知道更多的细节。谢谢,请看我的答案。是的,那很好,但这怎么可能是最好的方法呢?请解释一下,学习新东西对我来说真的很棒