在表格视图单元格中有一个按钮可以打印一些东西

问题描述 投票:0回答:1

在我下面的快速代码中,它在表格视图单元格中有一个按钮。问题是当我单击按钮时没有打印任何内容。我不知道下一步该做什么,按钮已连接并且应该可以工作。我的代码不使用故事板及其所有代码。所以不存在故事板连接问题

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let tableView = UITableView()
    let names = ["Katy Perry", "Jessica Biel", "Taylor Swift"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.frame = view.bounds
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(tableView)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
        
        let name = names[indexPath.row]
        cell.textLabel?.text = name
        cell.button.tag = indexPath.row // Set tag to identify which button is tapped
        cell.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        
        return cell
    }
    
    @objc func buttonTapped(_ sender: UIButton) {
        let index = sender.tag
        let selectedName = names[index]
        print("Button tapped in cell for \(selectedName)")
    }
}

class CustomTableViewCell: UITableViewCell {
    let button = UIButton(type: .system)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        button.setTitle("Tap", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        
        addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerYAnchor.constraint(equalTo: centerYAnchor),
            button.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16)
        ])
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func buttonAction() {
        print("Button in cell tapped")
    }
}
swift uitableview button func nstableviewcell
1个回答
-1
投票

将单元格的

isUserInteractionEnabled
上的
false
设置为
contentView
,无论是在单元格的
init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
中还是在表格视图的
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.contentView.isUserInteractionEnabled = false
        ...
    }
}

class CustomTableViewCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        ...
        contentView.isUserInteractionEnabled = false
        ...
    }
}

输出:

点击单元格中的按钮
在牢房中为凯蒂·佩里轻按按钮

© www.soinside.com 2019 - 2024. All rights reserved.