【问题标题】:How to add an action button to a custom XIB cell?如何将操作按钮添加到自定义 XIB 单元格?
【发布时间】:2021-04-25 10:59:42
【问题描述】:

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

到目前为止,我创建了一个带有按钮 (timeButton) 的自定义 XIB Cell (TaskListCell),并将 TaskListCell 注册到 TaskListViewController。对于 tableView(_:cellForRowAt:),我为 timeButton 和 addTarget(_:action:for:) 添加了 tag 以在 timeButton 为轻拍。但是,我收到此错误消息:线程 1:“-[Sprints.TaskListCellpressedTimeButton:]:无法识别的选择器已发送到实例 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) {
    }
}

这是在表格视图中显示自定义单元格的 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 {
}

【问题讨论】:

  • xib 文件名是什么?
  • 您必须添加断点,因为某些变量为零。这就是您的应用崩溃的原因。
  • @ElTomato xib文件名为TaskCell.xib
  • @luffy_064 我在: cell.timeButton.tag = indexPath.row 放置了一个断点,这显示 timeButton = (UIButton?) nil 。这是否意味着 timeButton 还不存在?
  • let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskListCell 看起来不对。

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


【解决方案1】:

检查您的单元格中的 IBAction 实例是否正在被调用。去掉这个函数就可以了

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多