我想在每个表格单元格中包含一个打开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
}
任何建议和指导都很棒。谢谢!
答案 0 :(得分:0)
这是我通常解决此问题的方式。为您的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)
}
}
然后,在控制器中,将其自身设置为委托,并通过适当的方法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中显示该URL。
答案 1 :(得分:-2)
快捷键4
这是使用indexPath
获得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
}
}
// ...
}
答案 2 :(得分:-2)
只需在SELECT Product_ID, Name
FROM Product
WHERE Category = 1
OR Category IN ( SELECT Cat_ID
FROM Categories
WHERE Subcategory_from is not null )
ORDER BY Product_ID;
中创建UIButton
对象,然后在viewDidLoad
函数中将该按钮添加为单元格上的子视图。按照您的要求拿伯顿的镜架。