【发布时间】:2014-10-30 19:21:37
【问题描述】:
嗨,我正在使用 UICollectionView,在 UICollectionViewCell 中我需要一张图片(图标)、一个标题和一些描述。我是 IOS 开发的新手,我希望 UICollectionViewCell 单元格高度取决于内容,因为可能是标题和图像将具有特定的高度,但描述具有不同的高度。谁能帮我计算描述标签的高度。我应该使用 textview 进行描述吗?
NSDictionary *service1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"Lorem ipsum dolor sit",@"title",
@"Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text",@"Description",
@"image1.png",@"image",
nil
];
NSDictionary *service2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"Lorem ipsum dolor sit", @"title",
@"Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit",@"Description",
@"image2.png",@"image",
nil
];
dataarray = [[NSMutableArray alloc] initWithObjects:service1, service2, nil];
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath] ;
UIImage * img = [UIImage imageNamed:[[dataarray objectAtIndex:indexPath.row] objectForKey:@"image"]];
UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,70,70)];
iv.image = img;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 50, 20)];
[titleLabel setText:[[dataarray objectAtIndex:indexPath.row] objectForKey:@"title"]];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont fontWithName:@"Arial" size:8.5];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
[cell addSubview:titleLabel];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UILabel *desclabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenWidth-20, 120)];
[desclabel setText:[[dataarray objectAtIndex:indexPath.row] objectForKey:@"Description"]];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont fontWithName:@"Arial" size:8.5];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentLeft;
[titleLabel sizeToFit];
[cell addSubview:titleLabel];
[cell addSubview:desclabel];
[cell addSubview:iv];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Adjust cell size for orientation
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
return CGSizeMake(screenWidth-10, 170.0f);
}
return CGSizeMake(screenWidth-10, 190.0f);
}
【问题讨论】:
-
@Manthan 我的描述文本有问题。如何查看描述标签的动态高度?
-
:而是计算描述文本的大小并直接传递该大小。
-
使用 UITableview 并检查此stackoverflow.com/questions/5161782/…
标签: ios objective-c uicollectionview uicollectionviewcell