【问题标题】:Displaying two different cells in a collection view - Swift 2.0 iOS在集合视图中显示两个不同的单元格 - Swift 2.0 iOS
【发布时间】:2016-04-12 00:38:25
【问题描述】:

我正在开发一个“交易”应用程序,我希望在其中拥有静态数量的单元格。

加载时,用户会看到 5 个单元格,每个单元格都显示一个标有“添加”的标签。

添加“玩家”后,该单元格显示玩家信息,其他 4 个单元格仍显示“添加”标签。另一个是添加的,2个单元格有玩家信息,3个有“添加”

我玩得很开心。谁能指出我正确的方向?我有自定义标签设置,我认为我的逻辑可能只是关于如何正确执行此操作。

【问题讨论】:

  • 你能展示你尝试过的东西吗?你到底对哪一部分有困难?

标签: ios swift uitableview uicollectionview


【解决方案1】:

您需要在 viewController 中继承 UICollectionViewDelegateUICollectionViewDataSource 协议,然后您需要实现 numberOfItemsInSectioncellForItemAtIndexPath 函数。 除此之外,您需要在情节提要中创建两种类型的单元格并将它们子类化,在下面的代码中,我将假设您调用 AddedPlayerCellDefaultCell 您的单元格,我会假设每个单元格也有一个名为 labelText 的标签。

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;
            }
}

【讨论】:

    【解决方案2】:

    为了管理两种不同的细胞类型,您可以:

    1. 为您的收藏视图创建 2 个原型单元格。给一个标识符"Add" 和另一个"Info""Add" 单元原型将包含标签 "Add""Info" 单元原型将包含显示玩家信息的字段。
    2. 向您的类添加一个数组属性,以跟踪哪些单元格显示“添加”。 var showingAdd = [true, true, true, true, true]
    3. cellForItemAtIndexPath 中,检查showingAdd 数组以确定出列单元格时使用哪个标识符:

      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
          ...
      }
      
    4. didSelectItemAtIndexPath中的单元格被选中时,检查它是否显示添加,然后进行相应的处理:

      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])
      }
      

    【讨论】:

    • 这是一种超级有趣的方式,我觉得很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多