我是初学者,可以学习ios。很高兴来到这里!我在使用UICollectionView时遇到了一些问题。
我已在myUICollectionCell.m中添加此内容并将该项目链接到storyBoard
#import "QuartzCore/QuartzCore.h"
@implementation myCollectionViewCell
@synthesize myImageView
-(instancetype)initWithFrame:(CGRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 60, 60)];
myImageView.layer.cornerRadius = view2.frame.size.width/2;
myImageView.layer.masksToBounds = YES;
[self.contentView addSubview:myImageView];
}
return self;
}
@end
ViewController.m
#import "myCollectionViewCell.h"
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = @"cell";
myCollectionViewCell *cell = (myCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.myImageview.image = [UIImage imageNamed:@"2.jpg"];
return cell;
}
myImageView仍然没有像我描述的那样变成圆圈。 它只能在我将描述放入collectionView:cellForItemAtIndexPath:
时才能工作-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = @"cell";
myCollectionViewCell *cell = (myCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.myImageview.image = [UIImage imageNamed:@"2.jpg"];
myImageView.layer.cornerRadius = view2.frame.size.width/2;
myImageView.layer.masksToBounds = YES;
return cell;
}
为什么?我和我应该在我的牢房中描述我的观点的地方混淆了。
答案 0 :(得分:1)
您似乎正在使用xib或storyboard,因此它不会调用initWithFrame:
的{{1}}。
改为使用UICollectionViewCell
:
awakeFromNib