Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当有空格时,防止uicollectionview单元格窗体向右移动--SWIFT_Swift_Uicollectionview_Uicollectionviewcell_Uicollectionviewlayout - Fatal编程技术网

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

当有空格时,防止uicollectionview单元格窗体向右移动--SWIFT,swift,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我的问题是,当下一个单元格跳到第二行时,uicollectionview单元格正在向左移动,而第一行中有空间(不适合容纳下一个单元格的空间),我如何防止单元格向右移动,即使还有空间,我也需要设置单元格之间的最大空间之类的东西(哪些不存在) 这是我的密码: func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVi

我的问题是,当下一个单元格跳到第二行时,uicollectionview单元格正在向左移动,而第一行中有空间(不适合容纳下一个单元格的空间),我如何防止单元格向右移动,即使还有空间,我也需要设置单元格之间的最大空间之类的东西(哪些不存在)

这是我的密码:

 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
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indepath:nsindepath)->UICollectionViewCell{
让cell=mycollectionView.dequeueReusableCellWithReuseIdentifier(“CharakterCell”,forIndexPath:indexPath)作为CharakterCollectionViewCell
对于(var i=0;i CGSize){
变量大小=CGSize(宽度:0,高度:0)
var label=UILabel()
如果(selectedCharInCollectionNames[indexPath.row]=“”)
{
尺寸=CGSize(宽:40,高:30)
}
其他的
{
label.text=selectedCharInCollectionNames[indexPath.row]
label.sizeToFit()
变量宽度=label.frame.width
尺寸=CGSize(宽度:(宽度+25),高度:30)
}
label.sizeToFit()
返回大小
}
私有let sectionInsets=UIEdgeInsets(顶部:10,左侧:10,底部:10,右侧:10)
func collectionView(collectionView:UICollectionView!,
布局集合视图布局:UICollectionViewLayout!,
insetForSectionAtIndex节:Int)->UIEdgeInsets{
返回部分插图
}
@Kevin R答案!解决了我的问题

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