当表视图单元格快速展开和折叠时,如何在imageview中更改图像



我为单元格使用了两个图像,一个在表视图单元格展开时显示,另一个在单元格折叠时显示

当细胞扩增时,我使用arrow-bottom

当细胞崩溃时arrow-right

对于细胞扩展和折叠,我使用以下代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath) as! FAQTableVIewCell
if selectedIndex == indexPath.row{
if self.isCollapse == false{

cell.arrowImage.image = UIImage(named: "arrow-rightt")
self.isCollapse = true
}else{
cell.arrowImage.image = UIImage(named: "arrow-bottom")
self.isCollapse = false
}
}else{
self.isCollapse = true
}
self.selectedIndex = indexPath.row
tableView.reloadRows(at: [indexPath], with: .automatic)
}

但是上面的代码不能正常工作,第一次当我扩展单元格时,它没有显示底部箭头,如果我扩展两个单元格,那么它也没有正确显示,请帮助代码

我需要当细胞膨胀时,我需要显示底部箭头,当细胞处于正常状态时(回到其原始位置或折叠位置(,然后我需要显示右箭头

当您重新加载行时,它们会进入cellForRowAt方法。

您必须在此方法中应用更改。存储折叠的indexPath并将其签入方法。

带阵列:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//use your cell identifier
let cell = tableView.dequeueReusableCell(withIdentifier: "FAQTableVIewCell", for: indexPath) as! FAQTableVIewCell
let image = collapsedCells.contains(indexPath) ? UIImage(named: "arrow-rightt") : UIImage(named: "arrow-bottom")

cell.image = image

//other settings

return cell
}
var collapsedCells = [IndexPath]()
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//use your height values instead 44 and 120
return collapsedCells.contains(indexPath) ? 44 : 120
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let index = collapsedCells.firstIndex(of: indexPath) {
collapsedCells.remove(at: index)
} else {
collapsedCells.append(indexPath)
}

tableView.reloadRows(at: [indexPath], with: .automatic)
}

最新更新