[英]How to change button image on tapped in repeating cell?
我正在做待办事项应用程序,我需要对按钮进行操作,点击该按钮将更改我点击的单元格中的图像。 单元从Firebase检索数据到UITableView。 我使用了按钮和图像插座,并且都报告了此错误:
从TaskViewController到UIButton的按钮无效。 插座不能连接到重复内容。
我有这段代码用于检索数据到单元格:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TasksCell", for: indexPath) as! TasksCell
let arrayTasks = tasks[indexPath.row]
cell.taskName.text = arrayTasks.content
return cell
}
我在情节提要中有这个
标签左侧的对象是button。
按钮图片设置在情节提要中。
我不知道该怎么做,因为我经验不足。 请帮我。
尝试:
Class
设置为TasksCell
(在我的示例中为WorkTableViewCell
)。 Module
字段中单击,将其清空,然后按Enter。 它应该更改为Current - project_name_here
(例如, Current - Artistry
。 这是它的外观截图。 检查它以确保您以正确的顺序选择了正确的项目:
然后,从连接检查器中删除单元的旧插座(按钮,标签,所有内容)。 在选择单元格时,然后在选择视图控制器时执行该步骤(但在选择视图控制器时, 仅删除“完成”按钮的插座)。
最后,通过打开TasksCell
类的代码(在新窗口中)而不是 TaskViewController
类(可能在TasksCell.swift
),并直接在下面的行中添加代码 :
class TasksCell: UITableViewCell {
(或类似),按住控件并拖动按钮,标签等,如下所示:
最后,完成仅从标签,按钮 等到TasksCell类的新插座的创建( 如前所述) (按“连接”之前,请参阅下面的注释):
根据需要更改Name
。 标签可以是taskName
,按钮可以是completedButton
。 请勿更改“ Type
或任何其他字段。 按“连接”,问题代码便可以正常工作。
在TasksCell类中,将按钮连接到单元格,您可以更轻松地访问它(是的,同意@ingraham,您不能将其连接到视图控制器):
class TasksCell: UITableViewCell {
@IBOutlet weak var buttonCheckbox: UIButton!
@IBOutlet weak var taskName: UILabel!
@IBOutlet weak var updatedDate: UILabel!
}
您可以使用图像视图作为按钮。 您也可以使用此方法更改图像
override func tableView(_ tableView: UITableView, didSelec tRowAt indexPath: IndexPath) {}
编辑:
我做了一个表格视图单元格类:
class TableViewCell: UITableViewCell {
@IBAction func bAction(_ sender: Any) {
button.titleLabel?.text = "asdaf"
}
@IBOutlet weak var button: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
然后我编辑tableView函数:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TableViewCell! = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as? TableViewCell
cell.textLabel?.text = "abc"
// Configure the cell...
cell.bAction(Any.self)
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.