【发布时间】:2018-07-06 13:51:50
【问题描述】:
目前我的项目有一个表格视图控制器,每行都有一个按钮和文本。单击按钮后,我无法弄清楚如何更改特定行上的按钮图像。我已经有函数“覆盖 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
}
【问题讨论】:
-
为什么不子类化按钮并覆盖
setSelected方法。您不需要像这样跟踪按钮。 -
你为什么不继承 UITableViewCell 并在那里拥有所有逻辑?
-
我已经有函数“覆盖 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)”将我的应用程序带到一个新视图。我需要按钮(单击时)将特定行上的数据添加到收藏夹变量。然后将显示在不同的屏幕上。我的按钮不会改变视图。
-
@Brandi 你在原帖里问的和你现在说的完全不同。
-
@Desdenova 我将相应地编辑我的原始帖子。抱歉,这是我第一次提问。
标签: ios swift uitableview uibutton