我的 UIViewController 类中有一个 Collection View ,并使用 bool 数组来查看哪些索引已展开 (refer to this answer) .
var isExpanded = [Bool]()
因此,当我单击按钮时,我可以使用以下命令重新加载单元格:
@objc func topButtonTouched(indexPath: IndexPath) {
print(indexPath)
isExpanded[indexPath.row] = !isExpanded[indexPath.row]
UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1.0, options: UIView.AnimationOptions.transitionCurlUp, animations: {
self.listCollectionView.reloadItems(at: [indexPath])
}, completion: { success in
print("success")
})
}
到目前为止一切顺利。但是,当单元格重新排序时,我遇到的问题就会出现。我不会使用 beginInteractiveMovementForItem()、updateInteractiveMovementTargetPosition() 等重新排序。
我开始使用这个功能(from this repository)下面创建一个缩放动画(单元格的快照),认为它也会对单元格重新排序:
@objc func longPressRecognized(_ recognizer: UILongPressGestureRecognizer) {
let location = recognizer.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: location)
switch recognizer.state {
case UIGestureRecognizer.State.began:
guard let indexPath = indexPath else { return }
let cell = cellForRow(at: indexPath)
snapshotView = cell.snapshotView(afterScreenUpdates: true)
collectionView.addSubview(snapshotView!)
cell.contentView.alpha = 0.0
UIView.animate(withDuration: 0.2, animations: {
self.snapshotView?.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
self.snapshotView?.alpha = 0.9
})
snapshotPanPoint = location
snapshotIndexPath = indexPath
case UIGestureRecognizer.State.changed:
guard let snapshotPanPoint = snapshotPanPoint else { return }
let translation = CGPoint(x: location.x - snapshotPanPoint.x, y: location.y - snapshotPanPoint.y)
snapshotView?.center.y += translation.y
self.snapshotPanPoint = location
guard let indexPath = indexPath else { return }
collectionView.moveItem(at: snapshotIndexPath!, to: indexPath)
snapshotIndexPath = indexPath
default:
guard let snapshotIndexPath = snapshotIndexPath else { return }
let cell = cellForRow(at: snapshotIndexPath)
UIView.animate(
withDuration: 0.2,
animations: {
self.snapshotView?.center = cell.center
self.snapshotView?.transform = CGAffineTransform.identity
self.snapshotView?.alpha = 1.0
},
completion: { finished in
cell.contentView.alpha = 1.0
self.snapshotView?.removeFromSuperview()
self.snapshotView = nil
})
self.snapshotIndexPath = nil
self.snapshotPanPoint = nil
}
}
我尝试更改开关的 .changed 部分中的 isExpanded 数组,但没有成功。我也尝试过这个answer .
我的问题的动图:
编辑: 之前,当我有一个 bool 数组时,它是这样的:
@objc func topButtonTouched(indexPath: IndexPath) {
isExpanded[indexPath.item] = !isExpanded[indexPath.item]
现在有了主要的对象数组,我正在尝试这样做:
@objc func topButtonTouched(indexPath: IndexPath) {
var names = mainData.map({ $0.isExpanded })
names[indexPath.row] = !names[indexPath.row]
然后更新索引路径。
最佳答案
一般来说,在单个 UITableView 中使用多个数据源会带来坏运气,当您在计划更改项目顺序 - 这简直就是自杀:)
因此,我建议您将数据源从两个数组 - [String]
和 [Bool]
更改为单个 [ListItem]
-
(这里我使用的是class
,但你也可以使用struct
,只要记住classes是由传递的引用,而结构通过值传递)
class ListItem{
var title: String
var isExpended: Bool
init(title: String, isExpended: Bool){
self.title = title
self.isExpended = isExpended
}
}
然后,当用户完成重新排序时,您需要在列表上提交这些更改 - 删除元素并将其插入到不同的索引即可完成这项工作。
关于swift - 如何在展开时保持 UICollectionviewcell 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53642418/