显示我的问题视频:问题
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
cell.lblName.text! = nameArr[indexPath.row]
cell.iconImg.image = UIImage(systemName: "chevron.down")
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.SelectedIndex == indexPath.row && isCollapse == true {
return 283
} else {
return 40
}
}
var select = false
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
tableView.deselectRow(at: indexPath, animated: true)
if SelectedIndex == indexPath.row {
if self.isCollapse == false {
self.isCollapse = true
} else {
self.isCollapse = false
}
} else {
self.isCollapse = true
}
self.SelectedIndex = indexPath.row
tableView.reloadRows(at: [indexPath], with: .automatic)
cell.iconImg.image = UIImage(systemName: "chevron.up"
}
我键入了如上所述的可扩展单元代码。我想将此图像chevron.down
(V(到cell.iconImg
到(^(。
我试过cell.isSelected
,但没用。我该如何解决这个问题?
只需在中调用reload tableview就可以在cellForRowAt方法中选择并更改图像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
cell.lblName.text! = nameArr[indexPath.row]
cell.iconImg.image = UIImage(systemName: self.isCollapse ? "chevron.down" : "chevron.down")
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.isCollapse == false {
self.isCollapse = true
} else {
self.isCollapse = false
}
tableView.reloadData()
}
您需要更新cellForRowAt的代码,如下所示。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
cell.lblName.text! = nameArr[indexPath.row]
Bool isExpand = (self.SelectedIndex == indexPath.row && self.isCollapse == true);
cell.iconImg.image = UIImage(systemName: isExpand ? "chevron.up" : "chevron.down")
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.SelectedIndex == indexPath.row && isCollapse == true {
return 283
} else {
return 40
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
tableView.deselectRow(at: indexPath, animated: true)
if SelectedIndex == indexPath.row {
if self.isCollapse == false {
self.isCollapse = true
} else {
self.isCollapse = false
}
} else {
self.isCollapse = true
}
tableView.reloadData();
}