我已经离开并在另一个集合视图中设置了一个集合视图,并且在大多数情况下它似乎正常工作。但是,尝试从我的代码的各个部分显示或编辑UI元素时会出现一些小故障。
在cellForItemAt中,我可以使用通常的格式在单元格中成功显示图像和其他UI元素。
在didSelectItemAt中,似乎无法操纵单元格的UI。以下面为例。当我选择一个单元格时,我想添加一个活动指标,这是非常标准的。我在其他地方做过这个:
AIV定义:
let activityIndicatorView : UIActivityIndicatorView = {
let aiv = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.white)
aiv.color = UIColor.black
aiv.translatesAutoresizingMaskIntoConstraints = false
aiv.isUserInteractionEnabled = false
aiv.startAnimating()
return aiv
}()
然后在didSelectItem中尝试在单元格中激活它:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell
cell.contentView.addSubview(self.activityIndicatorView)
self.activityIndicatorView.centerXAnchor.constraint(equalTo: cell.contentView.centerXAnchor).isActive = true
self.activityIndicatorView.centerYAnchor.constraint(equalTo: cell.contentView.centerYAnchor).isActive = true
// bunch of attempts to try display - no luck
cell.contentView.bringSubview(toFront: self.activityIndicatorView)
self.activityIndicatorView.isHidden = false
cell.setNeedsDisplay()
cell.setNeedsLayout()
collectionView.reloadItems(at: [indexPath])
没有任何显示。如果我打印到控制台
print(cell.contentView.subviews)
我收到了一些在Interface Builder中添加的东西,我看到活动指标被列为子视图:
<UIProgressView: 0x7ff280e1bf20; frame = (5 24; 40 2); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x60800003da80>>,
<UIImageView: 0x7ff280d3b940; frame = (0 0; 15 15); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x608000234ce0>>,
<PFImageView: 0x7ff280d3eb70; baseClass = UIImageView; frame = (0 0; 50 50); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x60800022b320>>,
<UIActivityIndicatorView: 0x7ff280c5df10; frame = (0 0; 20 20); userInteractionEnabled = NO; layer = <CALayer: 0x600000430280>>]
所以它在那里,但我真的很难尝试让它显示出来。