簡體   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