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

时间:2021-01-21 02:29:27

标签: swift uitableview xib custom-cell action-button

我想创建一个自定义的 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 {
}

1 个答案:

答案 0 :(得分:0)

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

@IBAction func pressedTimeButton(_ sender: UIButton) {
}
相关问题