动态改变collectionview单元格大小

Posted

技术标签:

【中文标题】动态改变collectionview单元格大小【英文标题】:Dynamically change the collectionview cell size 【发布时间】:2016-01-01 04:58:27 【问题描述】:
func collectionView(collectionView: UICollectionView,  cellForItemAtIndexPath indexPath: NSIndexPath) ->  UICollectionViewCell
 
         MBProgressHUD.hideHUDForView(self.view, animated: true)

        var url : String
              let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! celll
            let imagess : UIImageView = cell.image

          url = self.imageArray.objectAtIndex(indexPath.row).valueForKey("photo-url-500") as! String
         [imagess.sd_setImageWithURL(NSURL(string: url), placeholderImage: nil)]


          return cell


     

   func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize
       
                let cgsize : CGSize = CGSizeMake(CGFloat(A_size_width.objectAtIndex(indexPath.row) as! NSNumber), CGFloat(A_size_height.objectAtIndex(indexPath.row) as! NSNumber))
               return cgsize
           


 func scrollViewDidEndDecelerating(scrollView: UIScrollView)
 
  let bottomFloat : CGFloat = scrollView.contentOffset.y + scrollView.frame.height
  if bottomFloat >= scrollView.contentSize.height
  
     let str   = (json.valueForKey("posts-total") as! NSNumber)
     let value:Int  = Int(str)

     if (StartNumber < value)
     
        reachedEnd = false;
        self.jsonParsingFromURL(StartNumber)
        StartNumber = StartNumber + 30;

     
     else
     
        reachedEnd = true;
        [self.IMG_collection .reloadSections(NSIndexSet(index: 0))]
     

  







 func fileheight_width()

  let qualityOfServiceClass = QOS_CLASS_BACKGROUND
  let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
  dispatch_async(backgroundQueue, 

     dispatch_async(dispatch_get_main_queue(),  () -> Void in

        if self.StartNumber == self.imageArray.count
        
        for var i = 0 ; i < self.imageArray.count ; i++
        
           let str :String = self.imageArray.objectAtIndex(i).valueForKey("photo-url-500") as! String
           let Imagesource : CGImageSourceRef = CGImageSourceCreateWithURL(NSURL(string: str)!, nil)!
           let pixelHeight = kCGImagePropertyPixelHeight as String
           let pixelWidth = kCGImagePropertyPixelWidth as String
           let imgProperty = CGImageSourceCopyPropertiesAtIndex(Imagesource, 0, nil) as NSDictionary!
           let height = imgProperty[pixelHeight] as! CGFloat!
           let width = imgProperty[pixelWidth] as! CGFloat!
           self.A_size_width.addObject(width)
           self.A_size_height.addObject(height)

        
     
        else
        
           for var i = 0 ; i < self.imageArray.count ; i++
           
              let str :String = self.imageArray.objectAtIndex(i).valueForKey("photo-url-500") as! String
              let Imagesource : CGImageSourceRef = CGImageSourceCreateWithURL(NSURL(string: str)!, nil)!
              let pixelHeight = kCGImagePropertyPixelHeight as String
              let pixelWidth = kCGImagePropertyPixelWidth as String
              let imgProperty = CGImageSourceCopyPropertiesAtIndex(Imagesource, 0, nil) as NSDictionary!
              let height = imgProperty[pixelHeight] as! CGFloat!
              let width = imgProperty[pixelWidth] as! CGFloat!
              self.A_size_width.addObject(width)
              self.A_size_height.addObject(height)

           

        
     )
     [self.IMG_collection.reloadData()]

  )

    

现在图像来自 Json 并存储在 imageArray 和 JSON 中,但是当我向下滚动集合视图时,然后在应用程序冻结 2-3 分钟后调用此方法func scrollViewDidEndDecelerating(scrollView: UIScrollView)。请给我一些解决方案。 并提前感谢您。

【问题讨论】:

【参考方案1】:

可以在 mainThread 中下载图片。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
    // do some task

dispatch_async(dispatch_get_main_queue(), ^
        // update some UI

    );
);

【讨论】:

亲爱的图片已经下载到你的 dispatch_async(dispatch_get_main_queue(), ^ // update some UI ); );但是当重新加载 collectioview 然后冻结应用程序。我仍然没有从服务器下载图像只设置大小。 你从 func fileheight_width() 获取图像大小对吗?如果是,那么您正在主线程上执行所有进程,如果 else 代码在后台,并且只在主线程中重新加载集合视图【参考方案2】:

你可以使用 CHTCollectionViewWaterfallLayout

https://github.com/chiahsien/CHTCollectionViewWaterfallLayout.

这就是我所做的

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    CatCollectionViewCell *catcollectionCell = [self.collectionCat dequeueReusableCellWithReuseIdentifier:@"catcell" forIndexPath:indexPath];
    if (catcollectionCell == nil) 
        [self.collectionCat registerNib:[UINib nibWithNibName:@"CatCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"catcell"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[[[self.arrayData valueForKey:@"get_post_details"] valueForKey:@"post_photo"] objectAtIndex:indexPath.row]]];
    [catcollectionCell.imageCat setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
        catcollectionCell.imageCat.image = image;
        if ([self.arraySize objectAtIndex:indexPath.row] == [NSNull null])

            [self.arraySize insertObject:[NSValue valueWithCGSize:CGSizeMake(image.size.width, image.size.height)] atIndex:indexPath.row ];
            [self.collectionCat reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]];
       
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) 

    ];
//    [catcollectionCell.imageCat setImageWithURL:[NSURL URLWithString:[[[self.arrayData valueForKey:@"get_post_details"] valueForKey:@"post_photo"] objectAtIndex:indexPath.row]]];
    return catcollectionCell;



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
        if ([self.arraySize objectAtIndex:indexPath.row] != [NSNull null] )
            CGSize size = [[self.arraySize objectAtIndex:indexPath.row] CGSizeValue];
            NSInteger imageHeight = (size.height /size.width)*(self.view.frame.size.width - 20 ) /2;
            return CGSizeMake((self.view.frame.size.width - 20 ) /2 , imageHeight);
        
        return CGSizeMake(150, 200);

//    return [self.arraySize[indexPath.item % 4] CGSizeValue];

希望这会有所帮助。

【讨论】:

亲爱的,我在这段代码中使用过,但它只在静态图像中起作用,当来自服务器端图像时不起作用。那个时候冻结我的应用程序 我在图像来自服务器时使用过它。它工作正常。你能告诉我你得到错误的问题吗?

以上是关于动态改变collectionview单元格大小的主要内容,如果未能解决你的问题,请参考以下文章

如何使用自动布局动态调整具有固定宽度的collectionview单元格高度?

集合视图单元格不会改变大小

如何通过CollectionView中的Flow Layout将单元格对齐到顶部

如何居中对齐 CollectionView 单元格?

集合视图更改单元格大小

普通 CollectionView 单元格中的动态 CollectionView 单元格