2

コレクションビューのヘッダーを設定したいので、メソッドを実装しましたfunc collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView

ただし、switchステートメントが機能しないようです。セクションに応じて分岐してヘッダービュー内にラベルを設定しようとしても、すべてのセクションの結果のヘッダービューには、私が書いたすべてのラベルが含まれています。

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionElementKindSectionHeader:
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath)
        let headerLabel = UILabel(frame: CGRectMake(2, 8, 120, 24))
        headerView.addSubview(headerLabel)

        print(indexPath.section)
        switch (indexPath.section) {
        case 0:
            headerLabel.text = "A"
            return headerView
        case 1:
            headerLabel.text = "B"
            return headerView
        default:
            break
        }

        return headerView

    default:
        assert(false, "Unexpected element kind")
    }
}

上記のコードでは、両方のセクションのラベルにABの両方のラベルがあり、互いに重なり合っています。

私の場合、スイッチが機能しないのはなぜですか?

私のコレクションビューのコンテンツはサーバーからデータをフェッチするため、print(indexPath.section)が 2 回実行され、それぞれが01がこの順序で出力されます。

これは問題に関連していますか?

4

2 に答える 2

-1

ケース1

サーバーからの応答を取得した後にのみ、コレクション ビューをリロードする必要があると思います。ビューでresponse/tableData配列を初期化すると表示されます。

ケース 2

スイッチ状態でサーバーからの応答配列データを試すことができます。

于 2015-12-09T06:07:01.630 に答える