2016-12-15 60 views
0

我在其中我正在收集观察室的宽度作为应用:iOS中UIcollectionviewCell的动态宽度?

- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 
    return UIEdgeInsetsMake(20, 20,20,20); 
} 
- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGRect screenRect = collection.frame; 
    CGFloat screenWidth = screenRect.size.width; 
    float cellWidth = screenWidth/count; 
} 

但现在我想对那些重要的单元格的宽度必须依赖于一个参数,如:

NSDictionary *dict=[arr objectAtIndex:indexPath.item]; 
    float duration=([[dict valueForKey:@"duration"]floatValue]); 

如果持续时间是45,那么它需要占用屏幕宽度的45%。任何人可以帮助我在这当我试图screenWidth * (duration/100.0)它看起来像这样

enter image description here

+0

float cellWidth = screenWidth/duration;试试这个 –

回答

0

如果您的CollectionView只是由这个细胞,那么我建议你去第方法。 每个项目有多个部分,每个部分只有一行。通过这种方法,即使行大小为10%或更少,部分中也只会有一行,并且与您想要的一样。

保持大小方法如下。

NSDictionary *dict=[arr objectAtIndex:indexPath.item]; 
float duration=([[dict valueForKey:@"duration"]floatValue]); 
float cellWidth = screenWidth * (duration/100.0); 
+0

,但它给了我连续2件物品的等宽度@Dev_Tandel – hacker

+0

是否有可能上传图片? –

+0

有时它是重叠的。你能告诉我如何指定临时间距吗? – hacker

0
- (CGSize)collectionView:(UICollectionView *)collectionView 
         layout:(UICollectionViewLayout *)collectionViewLayout 
     sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     CGRect screenRect = collection.frame; 
     CGFloat screenWidth = screenRect.size.width; 
     NSDictionary *dict=[arr objectAtIndex:indexPath.item]; 
     float duration=([[dict valueForKey:@"duration"]floatValue]); 
     float cellWidth = duration/100; 

return CGSize(cellWidth, 60); 
    } 

它可以帮助你。