UICollectionViewCells在重新排列后重置它们的大小

时间:2015-01-02 14:41:38

标签: swift ios8 uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个带有标签的单元格的UICollectionView,我希望能够通过拖放重新排序。

使用estimatedItemSize调整单元格大小,因为我希望它们具有不同的大小,具体取决于单元格中文本的大小。我可以让它显得很好。但是,一旦我使用 collectionView.moveItemAtIndexPath ,单元格似乎采用默认大小。

移动后直接重置collectionView的流程布局也没有修复。

viewDidLoad

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.estimatedItemSize = CGSize(width: 100, height: 100)
    collectionView.collectionViewLayout = layout

重新排序文字的 UIPanGestureRecognizer

func handlePan(sender: UIPanGestureRecognizer) {
    let locationPoint = sender.locationInView(collectionView)
    if sender.state == UIGestureRecognizerState.Began {

        if let indexPath = collectionView.indexPathForItemAtPoint(locationPoint)? {

            if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
                UIGraphicsBeginImageContext(cell.bounds.size)
                cell.layer.renderInContext(UIGraphicsGetCurrentContext())
                let cellImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()

                movingCellImage = UIImageView(image: cellImage)
                movingCellImage.center = locationPoint
                movingCellImage.transform = CGAffineTransformMakeScale(2, 2)
                collectionView.addSubview(movingCellImage)
                cell.alpha = 0
                movingCellOriginalIndexPath = indexPath

            }

        }

    } else if sender.state == UIGestureRecognizerState.Changed {
        movingCellImage.center = locationPoint

    } else if sender.state == UIGestureRecognizerState.Ended {

        if let indexPath = collectionView.indexPathForItemAtPoint(locationPoint)? {

            let cell = collectionView.cellForItemAtIndexPath(movingCellOriginalIndexPath)
            collectionView.moveItemAtIndexPath(movingCellOriginalIndexPath, toIndexPath: indexPath)
            cell?.alpha = 1

        }

        movingCellImage.removeFromSuperview()
    }
}

Before and after moving a UICollectionViewCell, having moved the second cell to the first

1 个答案:

答案 0 :(得分:1)

重新加载动画似乎解决了这个问题 之前的布局仍有一段时间......您可能需要创建自定义UICollectionViewLayout,而不是使用estimatedSize

if let indexPath = collectionView.indexPathForItemAtPoint(locationPoint)? {
    let cell = collectionView.cellForItemAtIndexPath(movingCellOriginalIndexPath)
    collectionView.moveItemAtIndexPath(movingCellOriginalIndexPath, toIndexPath: indexPath)
    cell?.alpha = 1

    collectionView.reloadSections(NSIndexSet(index: 0)) // not reloadData
}

<强> [UPDATE]

正如您所指出的,关键是-collectionView:layout:sizeForItemAtIndexPath:

  • 不要将estimatedSize设置为UICollectionViewFlowLayout
  • 在上面的委托方法中计算每个单元格大小
像这样:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let str = data[indexPath.item] // string to show in label

    // calculate size. you can use -boundingRectWithSize: as well
    var txtsize = (str as NSString).sizeWithAttributes([ NSFontAttributeName : UIFont.systemFontOfSize(16)]) // specify label's font
    txtsize.width += 10 // add margin if necessary
    txtsize.height = 100 // calculated text height or any value you want

    return txtsize
}