gpt4 book ai didi

ios - UICollectionViewCell 的动态高度取决于内容

转载 作者:行者123 更新时间:2023-11-29 12:34:54 25 4
gpt4 key购买 nike

您好,我正在使用 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);
}

最佳答案

好的,现在我明白了。您需要动态计算多行 UITextView 高度并根据它增加默认的 UITableViewCell 行高。

尝试使用这样的东西。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath)path
{
// take the text value
NSString *description = service1[@"title"];
// initial rect value
CGRect textFrame = CGRectMake(0,0,cellWidth,10000); // width value is enough for this operation
UITextView *myTextView = [[UITextView alloc] initWithFrame:textFrame]; // create textview
// set text view attributes as multiline, font, font size etc.
myTextView.text = description;
CGSize size = [myTextView sizeThatFits:myTextView.frame.size]; // that's all
CGFloat height = size.height + someDefaultCellHeight;
return height;
}

您可以在 -(void)viewDidLoad 方法中创建一些私有(private)的 UITextView 成员,以免每次 heightForRow 委托(delegate)方法调用时都创建它.

关于ios - UICollectionViewCell 的动态高度取决于内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26651040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com