集合视图向下移动单元格

collection view moving cell down

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

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
}

您可以更改左侧单元格的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
}

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

您似乎在此处使用了 UICollectionViewFlowLayout。

这是该布局的行为。

如果您想创建一个布局来满足您的需求,最好的办法是自己创建。

有很多关于创建您自己的布局的教程。根据您的具体需求,布局可能会有很大不同。

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