UICollectionView单元格中的宽度不正确

时间:2016-05-13 14:27:12

标签: ios objective-c swift uicollectionview uicollectionviewcell

每行需要2个单元格。

恰好在控制器宽度的中间位置。

在iPhone 5的情况下。屏幕宽度为import csv import time from datetime import date year = str(date.today().year) filename = r'output\Year\{}.csv'.format(year) reader = csv.reader(open(r"input.csv"),delimiter=',') filtered = filter(lambda p: year == p[7], reader) csv.writer(open(filename, 'w', newline=''), delimiter=',').writerows(filtered) time.sleep(0.1) ,每行为320

160

但是当绘制CollectionView时,单元格会被分开,就像下一张图像一样:

enter image description here

我的CollectionView配置是:

enter image description here

  

当我将self.view.frame.size.width => 320.0 midleWidth => 160.0 作为单元格的宽度时,按预期采用屏幕的所有宽度。

3 个答案:

答案 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
}

答案 1 :(得分: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;
}

答案 2 :(得分: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
    }