【发布时间】:2014-01-03 11:16:01
【问题描述】:
我有一个带有自定义单元格的 UICollectionView。在我尝试以编程方式而不是从情节提要创建集合视图之前,我已经完全完成了这项工作。我这样做是因为我无法针对不同的屏幕尺寸更改集合视图的框架尺寸。所以我尝试以编程方式创建一个集合视图。我的问题是我的自定义单元格不起作用。
对于我的自定义单元格,我在视图中画了一个圆圈,用户 cornerradius 在单元格的视图上,有一个 UILabel 和一个 UIImageView。
我画了一个圆圈并在自定义单元格的drawRect: 中执行cornerradius 的事情。其余的,即:将图像放在 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];
这是我的 customCell.m 的 drawRect:
- (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;
我认为我的数据没有问题,因为我所做的只是以编程方式创建集合视图,而不是使用情节提要。
编辑:: 我必须为自定义单元格分配初始化图像视图和标签,然后将它们添加为子视图。现在我的 4 个问题中的 3 个已经解决了。我仍然无法让拐角半径为我工作。我想为我的单元格设置一个稍微弯曲的边框
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout