防止 uicollectionview 单元格窗体在有空间时向右移动 - SWIFT

Posted

技术标签:

【中文标题】防止 uicollectionview 单元格窗体在有空间时向右移动 - SWIFT【英文标题】:prevent the uicollectionview cell form moving to the right when there is space -- SWIFT 【发布时间】:2015-04-01 09:02:12 【问题描述】:

我的问题是,当下一个单元格跳转到第二行并且第一行中有空间(不适合包含下一个单元格的空间)时,uicollectionview 单元格正在向左移动,我该如何防止该单元格向右移动,即使还有空间,我也需要一些单元格之间的 setMaximumSpace(不存在)?

这是我的代码:

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    let cell = mycollectionView.dequeueReusableCellWithReuseIdentifier("CharakterCell", forIndexPath: indexPath) as CharakterCollectionViewCell

    for (var i=0 ; i<4 ; i++)
    
        if (collectionCellIndex[i] == nil)
        
            collectionCellIndex[i] = indexPath
            println("\(collectionCellIndex[i])")
            println(i)
            break
        
    

    return cell



func collectionView(collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 
    var size = CGSize(width: 0, height: 0)
    var label = UILabel()
    if (selectedCharInCollectionNames[indexPath.row] == "")
    
        size = CGSize(width: 40, height: 30)

    
    else
    
        label.text = selectedCharInCollectionNames[indexPath.row]
        label.sizeToFit()
        var width = label.frame.width
        size = CGSize(width: (width+25), height: 30)

    
    label.sizeToFit()
    return size


private let sectionInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

func collectionView(collectionView: UICollectionView!,
    layout collectionViewLayout: UICollectionViewLayout!,
    insetForSectionAtIndex section: Int) -> UIEdgeInsets 


        return sectionInsets

【问题讨论】:

【参考方案1】:

@Kevin R 回答here!解决了我的问题

class AlignLeftFlowLayout: UICollectionViewFlowLayout 

var maximumCellSpacing = CGFloat(9.0)

override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? 
    let attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]

    for attributes in attributesToReturn ?? [] 
        if attributes.representedElementKind == nil 
            attributes.frame = self.layoutAttributesForItemAtIndexPath(attributes.indexPath).frame
        
    

    return attributesToReturn


override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! 
    let curAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)
    let sectionInset = (self.collectionView?.collectionViewLayout as UICollectionViewFlowLayout).sectionInset

    if indexPath.item == 0 
        let f = curAttributes.frame
        curAttributes.frame = CGRectMake(sectionInset.left, f.origin.y, f.size.width, f.size.height)
        return curAttributes
    

    let prevIndexPath = NSIndexPath(forItem: indexPath.item-1, inSection: indexPath.section)
    let prevFrame = self.layoutAttributesForItemAtIndexPath(prevIndexPath).frame
    let prevFrameRightPoint = prevFrame.origin.x + prevFrame.size.width + maximumCellSpacing

    let curFrame = curAttributes.frame
    let stretchedCurFrame = CGRectMake(0, curFrame.origin.y, self.collectionView!.frame.size.width, curFrame.size.height)

    if CGRectIntersectsRect(prevFrame, stretchedCurFrame) 
        curAttributes.frame = CGRectMake(prevFrameRightPoint, curFrame.origin.y, curFrame.size.width, curFrame.size.height)
     else 
        curAttributes.frame = CGRectMake(sectionInset.left, curFrame.origin.y, curFrame.size.width, curFrame.size.height)
    

    return curAttributes

【讨论】:

以上是关于防止 uicollectionview 单元格窗体在有空间时向右移动 - SWIFT的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionView 隐藏/防止单元格在 Sticky Header 后面滚动

防止在 DataGridView 中选择单元格

处理 UICollectionView 单元格上的滑动:在 UICollectionView 或每个单元格上实现处理程序?

防止 UICollectionView 中每个 UICollectionViewCell 中的内容在我滚动时发生变化?

UICollectionView 对齐单元格

UICollectionView中删除一个单元格后刷新单元格布局