奇妙な問題に遭遇しました: UIImageView を子として UICollectionViewCell をサブクラス化しました。このセルは描画されませんが (コレクション ビューはクリアに見えます)、タッチをキャッチします。また、Interface Builder では、子 UIImageView が表示されます。セルの 'drawRect:' メソッドをオーバーライドすると、カスタム描画コードが描画されます。
UIImageView が描画されない理由は何ですか? iOS 6 および 7 でテストしています。
次のコード:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
NSAssert (cell != nil, @"MyCollectionViewCell init failed"); // all right here
cell.iconImageView.image = [UIImage imageNamed:@"SomeImage"]; // this line doesn't matter
return cell;
}
...
@implementation LookCollectionViewCell
-(void) drawRect:(CGRect)rect
{
[super drawRect: rect];
[[UIColor colorWithRed: 1 green: 0 blue: 0 alpha:1] setFill];
UIRectFill(CGRectInset(rect, 10, 10)); // draws red rect on black background
}
@end
アップデート:
「initWithFrame:」を介してコードでコレクション ビュー セルを初期化し、「UIImageView」を手動で追加すると、すべて正常に動作します。問題は、Interface Builder のコレクション ビュー セルが機能しないことです。