【发布时间】:2017-01-16 16:56:35
【问题描述】:
我的 UICollectionViewCell 子类中出现了一个奇怪的视图,具有 2 个 imageView 和 1 个按钮的简单结构。
final class ProfileImageCell:UICollectionViewCell {
static var name: String { return "ProfileImageCell" }
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var anotherImageView: UIImageView!
func setup(...) { ... }
@IBAction func buttonAction(_ sender: UIButton) { ... }
}
在 setup() 方法中,我设置了 imageViews 并传递了一些属性。我不创建任何视图或更改自己的子视图。
然后在我的视图控制器中我像往常一样设置collectionView。
collectionView.register(UINib(nibName: ProfileImageCell.name, bundle: nil), forCellWithReuseIdentifier: ProfileImageCell.name)
ProfileImageCell.xib
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProfileImageCell.name, for: indexPath) as! ProfileImageCell
cell.setup(...)
return cell
}
这是奇怪的事情发生的时候。我的单元格中有 4 个子视图。 即使我在调用后立即停止执行:
(lldb) po cell.subviews
▿ 4 elements
- 0 : <UIImageView: 0x1026bac00; frame = (0 0; 375 667); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x170236dc0>>
- 1 : <UIImageView: 0x1026bade0; frame = (263 0; 112 112); autoresize = RM+BM; layer = <CALayer: 0x170237160>>
- 2 : <UIButton: 0x1026ba4d0; frame = (263 0; 112 112); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x170237140>>
- 3 : <UIView: 0x102732cb0; frame = (0 0; 600 600); gestureRecognizers = <NSArray: 0x17444e430>; layer = <CALayer: 0x174238aa0>>
有人知道那个 UIView 可能来自哪里吗?它有一个奇怪的框架(单元格出现在屏幕上后不会改变),覆盖了我的所有单元格,并且没有让任何手势通过按钮。另外,非常有趣的是,为什么它有一个手势识别器?
(lldb) po cell.subviews[3].gestureRecognizers?.first?.description
▿ Optional<String>
- some : "<UILongPressGestureRecognizer: 0x10271db10; state = Possible; view = <UIView 0x102732cb0>; target= <(action=_handleMenuGesture:, target=<Application.ProfileImageCell 0x1026ba790>)>>"
【问题讨论】:
标签: ios swift uicollectionview nib