【发布时间】:2019-12-04 21:44:37
【问题描述】:
Stackoverflow 用户。
我知道单元格可以打开 URL,否则 Button 可能无法在单元格内打开。 假设我正在应用程序上创建销售商品。我想点击那个按钮而不是单元格。
资源代码:
您已经创建了两个名为 DCCMerchViewController 和 MerchTableViewCell 的 swift 文件夹。
从情节提要连接 MerchTableViewCell(基于 tableview 单元格)
类 MerchTableViewCell: UITableViewCell {
// 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
}
}
现在,您已经从 Storyboard 连接到 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。谢谢!
【问题讨论】:
标签: arrays swift uitableview web button