集合视图向下移动单元格

时间:2018-07-12 05:48:10

标签: swift uicollectionview uicollectionviewcell

这是我的第一篇文章,尽管这看起来很简单,但我似乎找不到解决方法。我正在尝试将左侧的单元格向上移动,以匹配右侧的单元格的顶部。也许我需要使用其他控制器,但是我想我会在回到绘图板之前询问

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = .white
    collectionView?.isScrollEnabled = false
    collectionView?.contentInsetAdjustmentBehavior = .never
    collectionView?.register(LeagueCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 24
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 24
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.item == 0 {
        return CGSize(width: view.frame.width * 0.87, height: 70)
    } else if indexPath.item == 1 {
        return CGSize(width: 120, height: 120)
    } else if indexPath.item == 2 {
        return CGSize(width: view.frame.width * 0.47, height: view.frame.height * 0.6)
    } else if indexPath.item == 3 {
        return CGSize(width: view.frame.width * 0.3, height: view.frame.height * 0.41)
    }

    return CGSize(width: 10, height: 10)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 24, left: 24, bottom: 24, right: 24)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! LeagueCell
    return cell
}

2 个答案:

答案 0 :(得分:0)

您似乎在这里使用UICollectionViewFlowLayout。

这是该布局的行为。

如果您想创建一种可以满足您需求的布局,最好的办法就是自己创建。

有许多关于创建自己的布局的教程。取决于您想要的布局,可能会大不相同。

除了实际计算所有内容的prepare方法以外,它们几乎都以相同的方式工作。

答案 1 :(得分:-1)

您可以更改左侧单元格的Y并将其设置为与右侧单元格相同:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! LeagueCell

    if(indexPath.row == 1){

        let indexPathOfRightCell = IndexPath(row: 2, section: 0)
        let cellOnTheRight = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPathOfRightCell) as! LeagueCell
        cell.frame.origin.y = cellOnTheRight.frame.origin.y

    }

    return cell
}

这不是完美或标准的解决方案,但它可能适合您的情况。

相关问题