Arrays 如何在表格视图单元格中点击按钮以使用UIButton打开URL?
Stackoverflow用户 我知道单元格可以打开URL,否则按钮可能无法在单元格内打开。 让我们想象一下,我正在应用程序上创建一个销售内容。我想点击那个按钮而不是细胞 资源代码: 您已经创建了两个名为DCCMerchViewController和MerchTableViewCell的swift文件夹 从故事板连接MerchTableViewCell(基于tableview单元格) MerchTableViewCell类:UITableViewCell{Arrays 如何在表格视图单元格中点击按钮以使用UIButton打开URL?,arrays,swift,uitableview,web,button,Arrays,Swift,Uitableview,Web,Button,Stackoverflow用户 我知道单元格可以打开URL,否则按钮可能无法在单元格内打开。 让我们想象一下,我正在应用程序上创建一个销售内容。我想点击那个按钮而不是细胞 资源代码: 您已经创建了两个名为DCCMerchViewController和MerchTableViewCell的swift文件夹 从故事板连接MerchTableViewCell(基于tableview单元格) MerchTableViewCell类:UITableViewCell{ // Image @IBOutlet
// Image
@IBOutlet weak var Merch_Image: UIImageView!
// Title
@IBOutlet weak var Merch_Title: UILabel!
// Price
@IBOutlet weak var Merch_Price: UILabel!
// Purchase
@IBAction func PurchaseMerch(_ sender: Any, linker: String) {
UIApplication.shared.open(URL(string: linker)! as URL, options: [:], completionHandler: nil)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
现在,您已经从情节提要(基于表视图控制器)连接到DCCMerchViewController
类DCCMerchViewController:UIViewController{
@IBOutlet weak var MerchTableView: UITableView!
}
并创建一个数组,以允许使用IndexPath.row将数组调用到单元格中
class DCCMerchViewController: UIViewController {
@IBOutlet weak var MerchTableView: UITableView!
let imageMerch = ["image1", "image2"]
let titleMerch = ["Title1", "Title2"]
let buyLink = ["www.link.com", www.link1.com]
let priceMerch = ["44.99", "38.99", "23.99"]
override func viewDidLoad() {
super.viewDidLoad()
}
}
And add the extension after class Storetableview (notice make sure Table View Controller connect to this UITableViewDataSource and UITableViewDelegate.)
``` extension DCCMerchViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Image
return titleMerch.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 128
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let merchCell = MerchTableView.dequeueReusableCell(withIdentifier: "MerchCells", for: indexPath) as? MerchTableViewCell
// Image
merchCell?.Merch_Image.image = UIImage(named: imageMerch[indexPath.row])
// Title
merchCell?.Merch_Title.text = titleMerch[indexPath.row]
// Price
merchCell?.Merch_Price.text = "Price: $\( priceMerch[indexPath.row])"
// Purchase
merchCell?.PurchaseMerch( AnyIndex.self, linker: "\(buyLink[indexPath.row])")
// Kinda issues is merchCells cause this cells to open web othewise not tap those buttons... huh?
return merchCell!
}
}
现在有人知道如何在点击单元格时停止打开URL,我正在尝试使用按钮打开URL。谢谢!为单元格内的按钮创建出口,然后在
cellForRowAt
cell.btn.addTarget(self,#selector(clicked),for:touchUpInside)
cell.btn.tag = indexPath.row
将此添加到vc中
@objc func clicked (_ btn:UIButton) {
UIApplication.shared.open(URL(string:buyLink[btn.tag])!, options: [:], completionHandler: nil)
}