目前我的项目有一个Table View Controller,每行都有一个按钮和文本 . 单击按钮后,我无法确定如何更改特定行上按钮的图像 . 我已经将函数“覆盖func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)”用于将我的应用程序带到新视图 . 我需要按钮(单击时)将特定行的数据添加到收藏夹变量 . 然后将在不同的屏幕上显示 . 我的按钮不会改变视图 .
目前我有一个按钮事件函数,每次单击一行按钮时都会调用该函数 . 我遇到的问题是我不知道如何访问被单击的行中的特定按钮,只更改该按钮的图像 .
这是我的代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
// Setting Text and images
cell.batchName.text = processes[indexPath.item].batch_name
cell.startTime.text = processes[indexPath.item].starttime
cell.endTime.text = processes[indexPath.item].endtime
cell.slaTime.text = processes[indexPath.item].SLA
var statusColor = UIColor.black
// Calling Button event function
cell.starButton.addTarget(self, action: #selector(favoriteStarClicked), for: UIControlEvents.touchUpInside)
return cell
}
@IBAction func favoriteStarClicked(_ sender: Any) {
// Need to change the image of the button that was clicked
}
1 回答
现代Swift方式是一个回调闭包
在Interface Builder中,选择自定义单元格中的按钮,然后按⌥⌘4转到属性检查器 . 在弹出菜单
State Config
中选择Selected
,然后在Image
弹出窗口中选择star
图像(将图像保留为状态Default
为空) .在自定义单元格中,为按钮添加
callback
属性和操作(将其连接到按钮) . 图像通过按钮的isSelected
属性设置cellForRow
中根据isFavorite
设置图像并添加回调回调更新单元格中的模型和图像
没有协议/代表 .
没有自定义目标/操作 .
没有标签 .
没有索引路径 .
没有细胞框架数学 .