我的代码将城市数组读取到 tableView
。
单击单元格时,它会移动到
SecondViewController
。SecondViewController
有一个按钮。如果我单击该按钮,它将在该单元格中显示图像。
Problem: I am trying to update the cell whenever the button is clicked. It is working but no matter which cell is clicked, it always displays the image for cell 3, if I clicked again it displays for cell number 1 image.
如何解决此问题,以便在单击按钮时显示其单元格的图像,如果单击同一单元格,则隐藏图像。
我的代码:
var first = 0
var reload = false
var cellNumber: Int!
var cities:[String] = ["paris", "moscow", "milan","rome","madrid","garda","barcelona"]
@IBOutlet weak var tableView: UITableView!
// func to reload a cell
@objc func toReload(rowNumber: Int){
reload = true
let indexPath = IndexPath(row: rowNumber , section: 0)
tableView.reloadRows(at: [indexPath], with: .none)
}
// load tableview from a SecondViewController call
@objc func loadList(notification: NSNotification){
self.tableView.reloadData() // reload tableview
toReload(rowNumber: cellNumber)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cities.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else{
cell.myimage.isHidden = true
}
reload = false
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.cellNumber = indexPath.row
performSegue(withIdentifier: "send", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
// call load list method
}
第二 View Controller :
@IBAction func displayImage(_ sender: Any) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
最佳答案
您的 cellForRowAt
有问题。当 secondViewController 通知第一个时,无论您正在重新加载哪个单元格,cellForRowAt
总是会被调用,因为当您滚动 tableView 时想要recuse cell 并且 reload == true
对所有 cells 变为 true。所以你必须检查是否 indexPath.row == cellNumber
然后做剩下的工作:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if indexPath.row == cellNumber {
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else {
cell.myimage.isHidden = true
}
reload = false
}
}
return cell
}
关于ios - 更新 TableView 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245500/