2016-04-12 12 views
0

私は静的な数のセルを持ちたい "取引"アプリケーションに取り組んでいます。コレクションビューに2つの異なるセルを表示する - Swift 2.0 iOS

ロード時に、5つのセルが表示され、それぞれに「追加」というラベルが表示されます。

「プレーヤー」が追加されると、そのセルにプレイヤー情報が表示され、残りの4つのセルには「追加」ラベルが表示されます。もう1つは追加され、2つのセルにはプレイヤー情報があり、3つには「追加」があります

私はこれでかなり時間があります。誰かが正しい方向に私を向けることができますか?私はカスタムラベルの設定をしている、私は自分のロジックがちょうどこれを正しく実行する方法をオフになるかもしれないと思う。

+0

あなたがしようとしているかを示すことはできますか?どの部分が正確に問題を抱えていますか? – Paulw11

答えて

4

あなたは、あなたがnumberOfItemsInSectioncellForItemAtIndexPath機能を実装する必要があり、あなたのViewControllerにUICollectionViewDelegateUICollectionViewDataSourceプロトコルをサブクラス化する必要があります。 さらに、ストーリーボードに2種類のセルを作成し、サブクラス化する必要があります。AddedPlayerCellDefaultCell各セルには、というラベルが付いていると仮定します。 labelTextもあります。 2つの異なる細胞タイプを管理するために

let players = ["Player1","Player2"] //players added till now 
let numberOfCells = 5 

//Here you set the number of cell in your collectionView  
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
      return max(players.count,numberOfCells); 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
      if((indexPath.row + 1) < self.players.count){ //If index of cell is less than the number of players then display the player 

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("yourIdentifierForAddedPlayerCell", forIndexPath: indexPath) as! AddedPlayerCell 
        cell.labelText.text = self.players[indexPath.row] //Display player 
        return cell; 

      }else{//Else display DefaultCell 
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("yourIdentifierForDefaultCell", forIndexPath: indexPath) as! DefaultCell 
        cell.labelText.text = "Add" 
        return cell; 
      } 
} 
+0

美しい、ありがとう! – excessive34

0

、次のことができます。

  1. は、あなたのコレクションビューの2つのプロトタイプのセルを作成します。一方に識別子"Add"と他方に"Info"を与えます。 "Add"セルプロトタイプにはラベル"Add"が含まれ、"Info"セルプロトタイプにはプレーヤー情報を表示するフィールドが含まれます。
  2. クラスに「Add」と表示されているセルを追跡する配列プロパティを追加します。追加表示されているかどうかを確認し、

    let identifier = showingAdd[indexPath.row] ? "Add" : "Info" 
    let cell = dequeueReusableCellWithIdentifer(identifier...) 
    
    if !showingAdd[indexPath.row] { 
        // configure the cell with the proper player info 
        // retrieve info from info property array item created in 
        // step 4. 
        let player = playerInfo[indexPath.row] 
        cell.playerName = player.name 
        ... 
    } 
    
  3. セルがdidSelectItemAtIndexPathで選択され、それを処理する:cellForItemAtIndexPathvar showingAdd = [true, true, true, true, true]

  4. は、細胞をデキューするときに使用する識別子を決定するためにshowingAdd配列を確認しますしたがって:

    if showingAdd[indexPath.row] { 
        // query user to get player info 
        // store the info in a property array indexed by `indexPath.row` 
        playerInfo[indexPath.row] = PlayerInfo(name: name, ...) 
    
        showingAdd[indexPath.row] = false 
    
        // trigger a reload for this item 
        collectionView.reloadItemsAtIndexPaths([indexPath]) 
    } 
    
+0

これはこれを行う非常に面白い方法です、私はそれが魅力的だとわかります。 – excessive34

関連する問題