我正在开发一个“交易”应用程序,我希望拥有静态数量的单元格。
加载时,用户将看到 5 个单元格,每个单元格都显示一个标有“添加”的标签。
当添加“玩家”时,该单元格显示玩家信息,其他 4 个单元格仍然显示“添加”标签。另一个是添加的,2个单元格有玩家信息,3个单元格有“添加”
我对此感到非常痛苦。有人能指出我正确的方向吗?我有自定义标签设置,我认为我的逻辑可能只是关于如何正确执行此操作。
你需要子类化UICollectionViewDelegate and UICollectionView数据源协议在你的viewController中,那么你需要实现节中的项目数 and cellForItemAtIndexPath功能。
除此之外,您需要在故事板中创建两种类型的单元格并对它们进行子类化,在下面的代码中我假设您调用添加了玩家单元格 and 默认单元格你的细胞,我假设每个细胞都有一个名为标签文本 too.
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)