繁体   English   中英

带自定义UICollectionViewCell的UICollectionView

[英]UICollectionView with Custom UICollectionViewCell

我有一个自定义单元格的UICollectionView。 我完成了这项工作,直到我尝试以编程方式而不是故事板创建集合视图。 我这样做是因为我无法为不同的屏幕尺寸更改集合视图的帧大小。 所以我尝试以编程方式创建一个集合视图。 我的问题是我的自定义单元格不起作用。 对于我的自定义单元格,我在视图中绘制一个圆,单元格视图上的用户cornerradius ,有一个UILabel和一个UIImageView。

我绘制圆圈并在自定义单元格的drawRect:中执行cornerradius 其余的ie:将图像放在ImageView中,将文本放在UILabel中,我是在collectionView数据源方法中完成的。

我遇到的问题是我没有让cornerradius东西工作,虽然奇怪的是我可以在视图上画一个圆圈。 第二个问题是我没有在图像视图中获取图像而标签中没有文本

这是我的代码。 我在ViewDidload:方法中创建集合视图。

 [layout setSectionInset:UIEdgeInsetsMake(24, 10, 24, 10)];

 [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
 [layout setMinimumLineSpacing:15];
 [layout setMinimumInteritemSpacing:10];
 self.collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
 self.collectionView.delegate=self;
 self.collectionView.dataSource=self;
 [self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:@"cell"];
 self.collectionView.backgroundColor= [UIColor blackColor];
 self.searchBar.delegate = self;
 self.locations = [[NSArray alloc]init];
 self.location = [[NSString alloc]init];
 [self.view addSubview:self.collectionView];

这是drawRect:对于我的customCell.m

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *circularpath = [[UIBezierPath alloc]init];
    CGRect Rect = CGRectMake(6, 20, 130, 130);//338
    CGPoint mypoint = CGPointMake(Rect.origin.x + (Rect.size.width / 2), Rect.origin.y + (Rect.size.height / 2));
    NSLog(@"Circle center point::%f, %f", mypoint.x, mypoint.y);

    circularpath = [UIBezierPath bezierPathWithOvalInRect:Rect];
    circularpath.lineWidth = 3.0;
    [[UIColor whiteColor]setStroke];
    UIImage *ori = [UIImage imageNamed:@"drogba.jpg"];
    UIImage *image = [[UIImage alloc]initWithCGImage:ori.CGImage scale:1.45 orientation:UIImageOrientationUp];
    [[UIColor colorWithPatternImage:image]setFill];
    NSLog(@"Circular path:%@", circularpath);
    //this works
    [circularpath stroke];
    //this does not
    self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}

这是我的数据源方法

customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UIImage *image = [[UIImage alloc]init];
   if([self.allInfo count]>0){
       image = [self getImageForLocation:[self.allInfo objectAtIndex:indexPath.item]];
       NSDictionary *dict = [[NSDictionary alloc]initWithDictionary:[self.allInfo objectAtIndex:indexPath.item]];
       NSDictionary *city = [dict objectForKey:@"city"];
       NSString *name = [[NSString alloc]initWithString:[city objectForKey:@"name"]];
       cell.locationLabel.text = name;
       cell.imageView.image = [self getImageForSky:dict];
       cell.viewForBaselineLayout.backgroundColor = [UIColor colorWithPatternImage:image];
       cell.tempLabel.text = [self getTempForLocation:dict];
   }
NSLog(@"image description:%f", image.size.height);
count =1;
return cell;

我不认为这是我的数据的问题,因为我改变的是以编程方式创建Collection视图而不是使用storyboard。

编辑::我必须为图像视图和自定义单元格的标签分配init,然后将它们添加为子视图。 现在我解决了4个问题中的3个问题。 我仍然无法让角落半径为我工作。 我希望我的细胞有一个略微弯曲的边框

首先,因为我从故事板中删除了单元格,所以我不得不将视图作为子视图添加到单元格视图中。 其次,要获得圆角,我还必须将masksToBounds设置为YES所有这些必须在自定义单元格的initWithFrame:方法中完成。 这里是代码片段

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.imageView = [[UIImageView alloc]init];
        self.imageView.frame = CGRectMake(26, 27, 90, 90);
        self.tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(26, 125, 90, 21)];
        self.locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(11, 156, 121, 21)];
        self.tempLabel.textAlignment = NSTextAlignmentCenter;
        self.tempLabel.textColor = [UIColor whiteColor];
        self.locationLabel.textAlignment = NSTextAlignmentCenter;
        self.locationLabel.textColor = [UIColor whiteColor];

       [self.viewForBaselineLayout addSubview:self.imageView];
       [self.viewForBaselineLayout addSubview:self.tempLabel];
       [self.viewForBaselineLayout addSubview:self.locationLabel];

       self.viewForBaselineLayout.layer.masksToBounds = YES;
       self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
    }
    return self;
}

您的自定义单元格中有init方法

- (id)initWithFrame:(CGRect)frame

在此方法中更改角半径。 PS对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM