我在 .xib 文件中有一个 UIImageView,当用户选择一个集合视图单元格时,它的大小需要更改。.xib 文件被加载到视图控制器中,并有一个连接两者的 IBOutlet。当前的方法我在 UIView 上运行良好,但在 UIImageView 上失败了。
该代码仅在第一次加载屏幕时失败。如果用户选择任何 Collection View 单元格,UIImageView 更新就好了。
func displayDataForCell(index: Int) {
// Set the box fill amount for Anode, Feeder, and Wave
let anodePercentAmount = Double(pot.anode!)/100
let feederPercentAmount = Double(pot.feeder!)/100
let wavePercentAmount = Double(pot.wave!)/100
dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
dataView.waveBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
dataView.feederBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*anodePercentAmount)), width: 120.0, height: (180.0*anodePercentAmount))
self.dataView.feederBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*feederPercentAmount)), width: 120.0, height: (180.0*feederPercentAmount))
self.dataView.waveBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*wavePercentAmount)), width: 120.0, height: (180.0*wavePercentAmount))
})
}
我在函数collectionView(_, didSelectItemAtIndexPath indexPath:_)
方法中调用上述方法如下:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// This makes sure only one cell can be selected at once
if currentPotIndex != indexPath.row {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.highlighted
let cell0 = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: currentPotIndex, inSection: 0))
cell0?.highlighted = false
}
self.currentPotIndex = indexPath.row
displayDataForCell(currentPotIndex)
bottomCollection.reloadData()
}
最后,这是我目前尝试的解决方案:在函数中包含didSelectItemAtIndexPath
函数调用viewDidLoad()
。
//Initialize the view with the 0th indexed pot's data
collectionView(collectionView, didSelectItemAtIndexPath: NSIndexPath(forItem: currentPotIndex, inSection: 0))
self.dataView.layoutIfNeeded()
我会附上一张图片,但我没有足够的代表。感谢您的关注!