2013-10-17 61 views
1

我遇到了一個奇怪的問題:我已經將UICollectionViewCell與UIImageView作爲子對象。這個單元格不繪製(收集視圖看起來很清晰),但它捕捉到了觸動。此外,子UI UIImageView在Interface Builder中看起來可見。如果我重寫單元格的「drawRect:」方法,它會繪製我的自定義繪圖代碼。UICollectionViewCell不繪製子視圖

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中的collection view cell不起作用。

回答

0

如果UICollectionViewCell是Interface Builder中的UICollectionView的子項,看起來像這樣根本行不通。

從此答案:https://stackoverflow.com/a/15185028/326017「如果您使用的是Xib文件,則無法將UICollectionViewCell直接放入UiCollectionView中。」

因此,如果我使用單獨的XIB文件的單元格,並通過[collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];註冊,所有工作正常。

相關問題