首页 文章

UICollectionView,首次加载时图像大小不正确

提问于
浏览
2

我有一个非常简单的演示,它使用UICollectionView来显示图片列表,我使用一个包含UIImage和Label的自定义单元格类,如下所示,没有约束 .

customize cell on storyboard

然后我在代码中设置单元格的大小,如下所示 . 每行有3列 . 所有边距都设置为10 .

// Calculate size of cell
    func collectionView(collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                               sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let totalSpace = marginLeft + marginRight + (colomnSpacing * CGFloat(numberOfItemsPerRow - 1))
        let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(numberOfItemsPerRow))

        // 20 is for label height.
        return CGSize(width: size, height: size + 20)
    }

    // Section margin, if you only have one section, then this is the CollectionView's margin.
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: marginTop, left: marginLeft, bottom: marginBottom, right: marginRight) // top, left, bottom, right.
    }

    // Set row spacing
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return rowSpacing
    }

    // Set column spacing
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return colomnSpacing
    }

以下是用于设置图像和标签大小的代码 . 图像的宽度占屏幕宽度的1/3(不包括列空间),高度等于宽度 . 标签宽度等于图像宽度,标签的高度设置为20 .

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

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SearchImageCell

        // Set image size
        cell.imageView.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height - 20)
        cell.imageView.contentMode = .ScaleAspectFill


        let currentItem = self.responseArray[indexPath.row]
        let imageUrl = currentItem["imgurl"] as? String
        let url = NSURL(string: imageUrl!)

        // Load the image
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            let data = NSData(contentsOfURL: url!)
            dispatch_async(dispatch_get_main_queue(), { 
                cell.imageView.image = UIImage(data: data!)
            })
        }

        // Set label size
        cell.imageDesc.frame = CGRect(x: 0, y: cell.imageView.frame.height, width: cell.frame.width, height: 20)

        let shujia = currentItem["shujia"] as? String
        cell.imageDesc.text = shujia!
        cell.imageDesc.font = cell.imageDesc.font.fontWithSize(14)
        cell.imageDesc.textAlignment = .Center

        return cell
    }

The question is:

当我首先加载图像时,图像的大小不正确,如下图所示,您可以看到图像的高度占据了单元格的整个高度,并且标签未显示 .

enter image description here

但是,当我向下滚动(这使得单元格被重用)时,图像被调整大小,我得到了正确的结果,这就是我想要的!

enter image description here

我试图添加一些调试代码来打印单元格/图像/标签的大小,似乎所有大小都是正确的 .

cell width:  100.0
cell height:  120.0
image width:  100.0
image height:  100.0
label width:  100.0
label height:  20.0

那么,有什么不对?

在SO上有一些类似的问题(herehere),但答案对我不起作用,比如

1. cell.layoutIfNeeded()
2. self.collectionViewLayout.invalidateLayout()
3. self.collectionView?.layoutIfNeeded()

请帮忙,如果您需要更多代码请告诉我 .

1 回答

  • 0

    image viewlabel 设置 translatesAutoresizingMaskIntoConstraints 为true . 也许它有效 .

    注意:使用IB时,它会将它们设置为false

相关问题