OStack程序员社区-中国程序员成长平台

标题: ios - UICollectionview 根据图像高度设置单元格高度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:44
标题: ios - UICollectionview 根据图像高度设置单元格高度

嗨,我想根据从 url 获得的图像设置 UICollectionView 单元格的高度。谁能帮我?我检查了一些库,因为它们为单元格提供了随机高度。 In this one给单元格的随机高度。当我尝试给图像高度 like this它给了result .单元格之间出现空间。

也试过like this但单元格行之间没有工作空间。 see this



Best Answer-推荐答案


    Write in ViewDidLoad Method

    for (int i=0; i<[imageURLArray count]; i++)
        {
             //Declare Globally
            IMGArray1=[[NSMutableArray alloc] init];

            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[imageURLArray objectAtIndex:i]]];
            NSOperationQueue *urlQue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:urlRequest queue:urlQue completionHandler:^(NSURLResponse *response,
                                                                                                 NSData *MoblieDatadata, NSError *error)
             {
                 if ([MoblieDatadata length] >0 && error == nil)
                 {
                     UIImage *image = [[UIImage alloc]initWithData:MoblieDatadata];
                     [IMGArray1 addObject:image];

                 }
                 else if ([MoblieDatadata length] == 0 && error == nil)
                 {

                     NSLog(@"Nothing was downloaded.");



                 }
                 else if (error != nil)
                 {
                     NSLog(@"Error happened = %@", error);
                     dispatch_async(dispatch_get_main_queue(), ^{


                     });
                 }
             }];


    -(UICollectionViewCell *)collectionViewUICollectionView *)collectionView
                     cellForItemAtIndexPathNSIndexPath *)indexPath
    {
        examplemycell *myCell = [collectionView
                                        dequeueReusableCellWithReuseIdentifier"cell"
                                        forIndexPath:indexPath];

        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        myCell.imageView.image = image;

        return myCell;
    }
    #pragma mark -
    #pragma mark UICollectionViewFlowLayoutDelegate

    -(CGSize)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPathNSIndexPath *)indexPath
    {
        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        return image.size;
    }

//For setting spacing between collectionview cells
//use the delegate method like

- (CGFloat)collectionViewUICollectionView *)collectionView layoutUICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndexNSInteger)section  
  {
    return 10; // This is the minimum inter item spacing, can be more
  }

And Use this for verticle line spacing between cells

- (CGFloat)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

关于ios - UICollectionview 根据图像高度设置单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25786002/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4