在 Swift 5 中,我创建了一个 UITableViewCell 类并创建了一个 UIButton。
class DashboardingGameTableViewCell: UITableViewCell {
// MARK: Setup Views
var showInteractiveButton: UIButton = {
let interactive = UIButton()
interactive.setTitle("", for: .normal)
interactive.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
interactive.setImage(UIImage(systemName: "ellipsis"), for: .normal)
interactive.tintColor = .white
interactive.backgroundColor = .clear
interactive.translatesAutoresizingMaskIntoConstraints = false
return interactive
}()
...
我的
class DashboardingGameViewController: UIViewController, UITableViewDelegate, UITableViewDataSource...
UIButton 不可点击
因为它有效,我必须做......
关于 DashboardGameViewController:UITableViewCell...
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.isUserInteractionEnabled = false
addSubview(showInteractiveButton)
...
关于 DashboardGameViewController:UIViewController、UITableViewDelegate、UITableViewDataSource...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reusableIdentifier,
for: indexPath) as! DashboardingGameTableViewCell
cell.showInteractiveButton.addTarget(self, action: #selector(interactiveGames(_:)), for: .touchUpInside)
...
// MARK: - Private Funcs
@objc private func interactiveGames(_ button: UIButton) {
present(ShowInteractiveGameViewController(), animated: true, completion: nil)
}