1

私はiOSとSwiftの両方が初めてです。コレクション ビューを作成しましたが、正常に動作しますが、複数のアイテムを 1 行に表示したいと考えています。試しましたが、うまくいきません。誰かが私を助けることができますか?前もって感謝します。

これは私のコードです:

ダッシュボードコレクションVC

import UIKit

class DashBoardCollectionVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var main_collection_view: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = main_collection_view.dequeueReusableCell(withReuseIdentifier: "identify_collection_cell", for: indexPath) as! DashboardCollectionViewCell
        return cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        var collectionViewSize = collectionView.frame.size
        collectionViewSize.width = collectionViewSize.width/2.0 //Display Three elements in a row.
        collectionViewSize.height = collectionViewSize.height/4.0
        return collectionViewSize
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Item Clicked : \(indexPath.item)")

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    }

}

このように見えます

ここに画像の説明を入力

ContainerView の設計

ここに画像の説明を入力

2つのアイテムを並べて表示したい。どうすればそれを達成できますか?

4

3 に答える 3

1

セルの幅を計算する際に余白の幅を考慮してください。結果からマージン幅、つまり2つのセル間のスペースとセルの前後のスペースを差し引く必要があります
collectionViewSize.width = collectionViewSize.width/2.0。身長も同じ。

于 2017-09-20T11:20:32.847 に答える
0
Use this two methods it might work for you
=======================================

    func columnWidth(for collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout) -> CGFloat 
    {
                return self.clView.frame.size.width / 2
    }

func maximumNumberOfColumns(for collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout) -> Int {
                let numColumns: Int = Int(2.0)
                return numColumns
        }
于 2017-09-20T11:12:30.450 に答える