早上好
我创建了一个自定义 TableViewCell,它内部有一个 CollectionViewCell 和一个方向箭头。我能够使单元格展开和折叠,但是当我展开一个单元格并在前一个折叠单元格上单击另一个单元格时,我会喜欢它。示例:如果展开单元格并点击单元格,我希望单元格同时收缩和展开。这样在任何给定的时刻改变目标箭头时,只有一个细胞可以处于其展开状态。
例子:
如果 cellA 展开并且 cellB 被点击,我希望 cellA 收缩并且 cellB 同时展开。因此,在任何给定时刻改变目标箭头时,只有一个细胞可以处于其展开状态。
我的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch(selectedIndexPath)
{
case nil:
let cell = myTableView.cellForRow(at: indexPath) as! TableViewCell
cell.arrowImagem.image = UIImage(named:"content_arrow2")
selectedIndexPath = indexPath
default:
if selectedIndexPath! == indexPath
{
let cell = myTableView.cellForRow(at: indexPath) as! TableViewCell
cell.arrowImagem.image = UIImage(named:"content_arrow")
//cell.reloadInputViews()
selectedIndexPath = nil
}
}
self.myTableView.beginUpdates()
self.myTableView.reloadData()
//myTableView.reloadRows(at: [indexPath], with: .automatic)
self.myTableView.endUpdates()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let index = indexPath
if selectedIndexPath != nil{
if(index == selectedIndexPath)
{
return 147
}
else{
return 67
}
}
else
{
return 67
}
}
最佳答案
你可以这样做,但你需要做一些编码。首先检测何时选择了一行(将所选行的索引路径保存在变量中)。选择一行时,您将在从 didSelectRowAt 传递给 tableView reloadRows 的数组中添加新选定行的索引路径和先前选定行的索引路径(您在变量中有),此函数将强制重绘两行和 heightForRowAt 函数将被调用以获取两行的高度,在 heightForRowAt 中你将检查当前选定的行(你还记得我之前提到的变量吗?)并返回扩展高度,而对于其他行你返回折叠的高度。你应该得到你想要的结果。希望这会有所帮助。
关于ios - (Swift)点击时展开和折叠表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40663784/