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

娜迪亚·西迪卡(Nadia Siddiqah)

我想创建一个带有动作按钮的自定义XIB表视图单元格,该按钮可以设置到新的视图控制器。

到目前为止,我已经创建了一个带有按钮(timeButton)的自定义XIB单元格(TaskListCell ,并将TaskListCell注册到TaskListViewController对于tableView(_:cellForRowAt :),我为timeButton添加了标签,并添加了addTarget (_:action:for :)以在点击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 {
}

巴巴尔

从您的单元格检查IBAction实例是否正在被调用。删除此功能,它将起作用

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何为 pwa 添加自定义安装按钮

如何在操作栏上添加自定义按钮?

如何为 MKAnnotationView 使用自定义 XIB?

如何为已添加到 UIViewController 之上的 tableView 添加自定义单元格?

如何为“虚拟机操作员”添加自定义角色以设置自动关闭

如何添加自定义按钮状态

如何为ng2-smart-table添加新的自定义按钮

从框架加载自定义单元Xib

如何在handsontable中的单元格内添加自定义按钮?

如何在自定义单元格中添加按钮?

将自定义按钮操作添加到笔管

Django:如何向执行更改操作的更改表单页面添加自定义按钮?

如何在suiteCRM模块子面板列表中添加自定义操作按钮?

如何为操作栏创建自定义图标?

如何为MaterialUI按钮的“ startIcon”分配自定义图标?

如何为自定义标题样式添加阴影?

如何为istio Ingress网关添加自定义端口?

如何为Spring Boot 2添加自定义MeterRegisty

如何为Android自定义视图添加OnClick事件

如何为自定义chai断言添加typehint?

如何为requestretry添加自定义标头?

如何为@Column(nullable = false)添加自定义消息?

如何为Ember js添加自定义环境?

如何为该脚本添加自定义颜色?

如何为SyntaxNode添加自定义语法注释?

如何为特定行添加自定义clickHandler

如何为项目符号图添加自定义图例

如何为自定义的 nsis 安装程序添加边框

如何为用户添加自定义路线