0

我需要每行2個單元格。UICollectionView單元格中的寬度不正確

恰好在控制器寬度的中間。

在iPhone 5的情況下。屏幕寬度爲320,每行將爲160

self.view.frame.size.width => 320.0 
midleWidth => 160.0 

但當的CollectionView被drawed細胞是分離的,像下一個圖像:

enter image description here

我的CollectionView配置爲:

enter image description here

當我把self.view.frame.size.width作爲單元格的寬度,按預期採取屏幕的所有寬度。

+0

已在代碼中設置單元格間距任何地方? –

回答

2

試試這個下面的代碼

工作
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
     "Cell", forIndexPath: indexPath) 
    let viewsDictionary = ["View1": collectionView] 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[View1]|", options: [], metrics: nil, views: viewsDictionary)) 
    self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[View1]|", options: [], metrics: nil, views: viewsDictionary)) 

    return cell 
} 

func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize 
{ 
    let cellSize:CGSize = CGSizeMake(self.bounds.width/2, self.bounds.height) 
    return cellSize 
} 
+0

感謝aswer,Works,但是爲每個單元返回一個大的警告,與佈局問題有關。 '無法同時滿足約束和''和'' – jose920405

+0

嘗試添加此代碼 - cell.contentView.frame = cell.bounds cell.contentView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight] – ignotusverum

0

//使用這樣它適用於所有設備

- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    CGSize size; 
    CGRect bounds = [[UIScreen mainScreen] bounds]; 
    double bWidth = (bounds.size.width/320)* 200 ; // 200 is your cell width which given in xib 
    double bheight = (bWidth/320)* 250 // 250 is your cell height which given in xib; 
    size = CGSizeMake(bWidth, bheight); 

    return size; 
} 
+0

這行''(bounds.size.width/320)* 200'總是在iphone 5中是200. - >'(320/320)* 200 = 200'。而200不是屏幕的中間部分。如果通過'bounds.size.width/2'改變'200',單元格之間的相同空間再次出現 – jose920405

+0

您需要給出您的單元格寬度,我給出示例值 –

0

以上答案都不行所有設備試試這個

- (CGSize)collectionView:(UICollectionView *)collectionView 
          layout:(UICollectionViewLayout*)collectionViewLayout 
      sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

      CGSize windowSize=[[[UIApplication sharedApplication] delegate] window].frame.size; 

      return CGSizeMake(windowSize.width/2,heightOfCell); 
     } 
    - (CGFloat)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
minimumLineSpacingForSectionAtIndex:(NSInteger)section { 
return 0.0 
} 

    - (CGFloat)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
    return 0.0 
    } 
+0

感謝回答,但結果相同。相同的空間。 – jose920405

+0

@ jose920405嘗試編輯答案 –

+0

謝謝你,但不行。這2個函數是相同的,我在我的問題中添加了'我的CollectionView配置是:'。同樣嘗試,但沒有任何作用 – jose920405

相關問題