質問に答えるために@ Douglasのコメントを使用しましょう。だから私は完全にやるのを忘れて最初にすることは、ストーリーボードでのViewControllerにCTRL +ドラッグコレクションビューにしたとのdataSourceとデリゲートを有効にします。次に私のViewController.swiftで私は次のコードを実装しました:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(indexPath.item == 0) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "kitchenButton", for: indexPath)
return cell
} else if(indexPath.item == 1) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraButton", for: indexPath)
return cell
} else if(indexPath.item == 2) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "eventsButton", for: indexPath)
return cell
} else if(indexPath.item == 3) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mapButton", for: indexPath)
return cell
} else if(indexPath.item == 4) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "journeyButton", for: indexPath)
return cell
} else if(indexPath.item == 5) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "callButton", for: indexPath)
return cell
} else if(indexPath.item == 6) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "handsButton", for: indexPath)
return cell
} else if(indexPath.item == 7) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "swimButton", for: indexPath)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "none", for: indexPath)
return cell
}
}
次のコードは複数のセル用です。私はこのメソッドが私のために美しく働くように水平スクロール効果を達成しようとしています。
クラスのヘッダーには、UICollectionViewDelegateFlowLayout、UICollectionViewDataSourceを必ず含めてください。
ありがとう@ダグラス!
デリゲートとデータソースを設定するには、コレクションビューからvcにドラッグを制御しましたか?あなたのコレクションビューデリゲートメソッドにも従ってください。 – Douglas
@Douglas:はい、しました。私はもう少し研究をしました。私はそれをすることを忘れてしまったので、ありがとう!しかし、私は異なる識別子を持つ複数のセルを持っています(私は1つの識別子を使うことはできません)。 – insolence
cellforindexpathメソッドでは、おそらく1つの再利用識別子を指定しました。 8種類の細胞タイプがある場合は、順序を把握してifステートメントを使用する必要があります。インデックスパスが1の場合、識別子「firstcell」のセルを使用します。インデックスパスが2の場合は、別のパスを使用します。 – Douglas