簡體   English   中英

CollectionView不顯示圖標

[英]CollectionView doesn't display icons

第一次創建UICollectionView ..我缺少什么? 我已經嘗試了所有方法,圖像肯定在應用程序中,正確命名並在數組中找到。 但是,它們不會顯示:

我的IconCell的標題:

#import <UIKit/UIKit.h>

@interface IconSelectionCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@end

實現我的IconCell:

#import "IconSelectionCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation IconSelectionCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.restorationIdentifier = @"IconCell";
        self.backgroundColor = [UIColor clearColor];
        self.autoresizingMask = UIViewAutoresizingNone;

        CGFloat borderWidth = 1.0f;
        UIView *bgView = [[UIView alloc] initWithFrame:frame];
        bgView.layer.borderColor = [UIColor blueColor].CGColor;
        bgView.layer.borderWidth = borderWidth;
        self.selectedBackgroundView = bgView;

        CGRect myContentRect = CGRectInset(self.contentView.bounds, borderWidth, borderWidth);

        UIView *myContentView = [[UIView alloc] initWithFrame:myContentRect];
        myContentView.backgroundColor = [UIColor whiteColor];
        myContentView.layer.borderColor = [UIColor colorWithWhite:0.5f alpha:1.0f].CGColor;
        myContentView.layer.borderWidth = borderWidth;
        [self.contentView addSubview:myContentView];
    }
    return self;
}

@end

還有我的collectionViewController:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"IconCell";

    IconSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    cell.iconImageView.image = [UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];

    return cell;
}

我需要通過代碼將imageview添加到單元格嗎? 我剛剛在Interface Builder中創建了一個插座……或者我想念什么? 謝謝你的幫助!

我建議先嘗試使其以編程方式運行。 給您的IconSelectionCell類一個UIImageView的屬性(不是IBOutlet)。 然后在IconSelectionCellviewDidLoad方法中,在UIImageView屬性上設置以下屬性: imageframe (!)和contentMode

如果您確實要使用Interface Builder和情節提要,請確保插座正確連接,並且原型單元的身份檢查器的類字段中有您的自定義類名稱,並記住要設置圖像。 在程序上更容易,因為您可以布置斷點並查看代碼是否真正被調用以及何時調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM