如何向自定义 XIB 单元格添加操作按钮?

问题描述

我想创建一个自定义的 XIB 表格视图单元格,其中包含一个可以切换到新视图控制器的操作按钮。

到目前为止,我创建了一个带有按钮 (timeButton)自定义 XIB Cell (TaskListCell),并将 TaskListCell 注册TaskListViewController。对于 tableView(_:cellForRowAt:),我为 timeButton 添加tagaddTarget(_:action:for:) 以在 timeButton 时响应轻拍。但是,我收到此错误消息:线程 1:“-[Sprints.TaskListCell presstimeButton:]: unrecognized selector sent to instance 0x105311560”

这是自定义的 XIB 单元文件

class TaskListCell: UITableViewCell {

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var timeButton: UIButton!
    
    @IBAction func pressedTimeButton(_ sender: UIButton) {
    }
    
    override func awakeFromNib() {
    }
    override func setSelected(_ selected: Bool,animated: Bool) {
    }
}

enter image description here

这是在表格视图中显示自定义单元格的 ViewController:

class TaskListViewController: UIViewController {
    
    // MARK: - Outlet Variables
    @IBOutlet weak var taskList: SelfSizedTableView!
    ...
    
    // MARK: - Instance Variables
    var taskData = [TaskData]()
    var taskCount: Int = 1
    ...
    
    // MARK: - View Controller Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Register TaskListCell.xib file
        let taskCellNib = UINib(nibName: "TaskCell",bundle: nil)
        taskList.register(taskCellNib,forCellReuseIdentifier: "taskCell")
        ...
        
        // Connect table view's dataSource and delegate to current view controller
        taskList.delegate = self
        taskList.dataSource = self
    }
    
    // MARK: - Action Methods
    // Adds new cell in Table View
    @IBAction func pressedAddTask(_ sender: UIButton) {
        taskCount += 1
        taskList.reloadData()
        taskList.scrollToRow(at: IndexPath(row: taskCount-1,section: 0),at: .bottom,animated: true)
    }
    
    // MARK: - Methods
    // Segues to SelectTime screen when timeButton is pressed
    @objc func pressedTimeButton(_ sender: UIButton) {
        let destination = SelectTimeViewController()
        navigationController?.pushViewController(destination,animated: true)
    }
    
}

extension TaskListViewController: UITableViewDataSource {
    
    // Return the number of rows in table view
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return taskCount
    }
    
    // Return the cell to insert in table view
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell",for: indexPath) as! TaskListCell

        // Fatal error for next line: "Unexpectedly found nil while implicitly unwrapping an Optional value"
        cell.timeButton.tag = indexPath.row
        cell.timeButton.addTarget(self,action: #selector(pressedTimeButton(_:)),for: .touchUpInside)
        
        return cell
    }
    
}

extension TaskListViewController: UITableViewDelegate {
}

解决方法

从您的单元中检查 IBAction 的实例可能正在被调用。去掉这个功能就可以了

@IBAction func pressedTimeButton(_ sender: UIButton) {
}