【发布时间】:2018-02-20 06:29:54
【问题描述】:
我在 UITableViewCell 中有 UIButton。当我单击 UITableViewCell 中的 UIButton 时,多个 UIButtons 图像正在发生变化。假设,由于 Json Data 显示了 10 个 UIButton。接下来我将点击 UITableViewCell 中的 UIButton。结果在 UIButton 1、3、5、7、9 中改变了图像。但它应该改变一个 UIButton 图像。请帮我发布代码。我只想在单击 UIButton 时从 UITableViewCell 更改特定的 UIButton 图像
TableViewController
import UIKit
class HomeViewController2: UIViewController, UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var homeTableView: UITableView!
let arrNewsList = [[Home]]()
override func viewDidLoad() {
super.viewDidLoad()
self.homeTableView.delegate = self
self.homeTableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return arrNewsList.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrNewsList[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell2", for: indexPath) as! HomeTableViewCell2
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.view.frame.size.height
}
}
TableViewCell
class HomeTableViewCell2: UITableViewCell {
@IBOutlet weak var bookMarkBtn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
@IBAction func bookMarkBtnClick(_ sender: Any) {
guard let btnCheck = sender as? UIButton else {
return
}
btnCheck.setImage(UIImage(named: "favorite_blue"), for: UIControlState.normal)
}
}
【问题讨论】:
-
你得到了什么结果?
-
您是否为您的 json 响应创建了任何模型类对象?你能分享你的示例 json 吗?
-
这是出列可重用单元内存引起的问题,反映了这一点。
-
@NilomiShah 尝试帮助我
-
在您的自定义单元格中,尝试添加
prepareForReuse()并将按钮变量设置为 nil
标签: ios swift uitableview uibutton