2016-12-15 63 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); 
    } 

它可以幫助你。