Search code examples
swiftuitableviewxibcustom-cellaction-button

How to add an action button to a custom XIB cell?


I want to create a custom XIB Table View Cell with an action button that can segue to a new view controller.

So far, I created a custom XIB Cell (TaskListCell) with a button (timeButton), and registered the TaskListCell to TaskListViewController. For tableView(_:cellForRowAt:), I added tag for timeButton and addTarget(_:action:for:) to respond when timeButton is tapped. However, I get this error message: Thread 1: "-[Sprints.TaskListCell pressedTimeButton:]: unrecognized selector sent to instance 0x105311560"

Here is the custom XIB cell file:

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

Here is the ViewController that displays the custom cells in a Table View:

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 {
}


Solution

  • Check instance of IBAction from your cell may be it is being called. Remove this function and it will work

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