我有水平滚动CollectionView
未知数量的单元格,除了导航栏以外的整个屏幕。和CollectionViewCell
,其中整个CollectionView
。此外,在这个单元格内,我已经集中了UIView
以及一些标签和一个按钮。这是我的用户界面的样子:click。在CollectionViewCell中向上移动标签动画
每当用户点击此UIView中的按钮时,标签向上移动一点,并且所有其他标签都可见。如果这个模式已经被激活,按钮的工作原理相反的方式:
class WordCell: UICollectionViewCell {
var isActive = false
@IBOutlet weak var word: UILabel!
@IBOutlet weak var translation: UILabel!
@IBOutlet weak var exOne: UILabel!
@IBOutlet weak var exTwo: UILabel!
// ...
@IBAction func move(_ sender: UIButton) {
if !isActive {
UIView.animate(withDuration: 0.25, animations: {
self.word.frame.origin.y -= self.bounds.height/7
}, completion: nil)
UIView.animate(withDuration: 0.3, animations: {
self.translation.alpha = 1
self.exOne.alpha = 1
self.exTwo.alpha = 1
})
isActive = true
} else {
UIView.animate(withDuration: 0.25, animations: {
self.word.frame.origin.y += self.bounds.height/7
}, completion: nil)
UIView.animate(withDuration: 0.3, animations: {
self.translation.alpha = 0
self.exOne.alpha = 0
self.exTwo.alpha = 0
})
isActive = false
}
}
}
在那之后我的UI看起来像:click
我的CollectionView代码:
// CollectiomView Delegate stuff, in the code above I just retrieve data from FireBase
extension RepeatController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return words.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "wordCell", for: indexPath) as! WordCell
if words[0].lPairs == "re" {
cell.word.text = words[indexPath.row].translation
cell.translation.text = words[indexPath.row].word
} else {
cell.word.text = words[indexPath.row].word
cell.translation.text = words[indexPath.row].translation
}
cell.exOne.text = words[indexPath.row].exOne
cell.exTwo.text = words[indexPath.row].exTwo
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let bounds = collectionView.bounds
return CGSize(width: bounds.width, height: bounds.height)
}
}
问题:
此系统只有当我点击按钮两次(向上移动标签,然后将其返回给sa我定位并隐藏所有其他标签)。否则,在2-3个单元格之后,所有其他标签变得可见并且主标签位于顶部,就像我在每个单元格的按钮上键入的一样(但我没有)。问题是什么?
只是胡乱猜测:这可能是细胞重复使用的结果。通常TableView/CollectionView单元格是[回收/重用](https://stackoverflow.com/questions/24600645/uicollectionview-without-reusing-cells),数据源应该是分开的 - 它们只是表示形式。请参阅:[dequeueReusableCell Documentation](https://developer.apple.com/documentation/uikit/uicollectionview/1618063-dequeuereusablecell)以及[prepareForReuse](https://developer.apple.com/documentation/uikit/uicollectionreusableview/) 1620141-prepareforreuse)应该重置你的'WordCell'的方法。 – makadev
@Oleg Blanutsa无论何时更新UI,您都应该在DispatchQueue.main.async块中执行此操作,请在您的代码中进行此更改并告诉结果 – 3stud1ant3