如何向自定义 XIB 单元格添加操作按钮?
How to add an action button to a custom XIB cell?
我想创建一个自定义 XIB Table View Cell,带有一个可以切换到新视图控制器的操作按钮。
到目前为止,我创建了一个带有按钮 (timeButton) 的自定义 XIB Cell (TaskListCell),并将 TaskListCell 注册到 任务列表ViewController。对于 tableView(_:cellForRowAt:),我为 timeButton 添加了 tag 和 addTarget(_:action:for:) 在点击 timeButton 时响应。但是,我收到此错误消息:线程 1:“-[Sprints.TaskListCell pressedTimeButton:]:无法识别的选择器发送到实例 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) {
}
}
这是在 Table 视图中显示自定义单元格的 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) {
}
我想创建一个自定义 XIB Table View Cell,带有一个可以切换到新视图控制器的操作按钮。
到目前为止,我创建了一个带有按钮 (timeButton) 的自定义 XIB Cell (TaskListCell),并将 TaskListCell 注册到 任务列表ViewController。对于 tableView(_:cellForRowAt:),我为 timeButton 添加了 tag 和 addTarget(_:action:for:) 在点击 timeButton 时响应。但是,我收到此错误消息:线程 1:“-[Sprints.TaskListCell pressedTimeButton:]:无法识别的选择器发送到实例 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) {
}
}
这是在 Table 视图中显示自定义单元格的 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) {
}