UICollectionView第一个可重用的表格单元格未显示

时间:2018-06-24 20:44:34

标签: swift

我现在正在开发游戏,而我的关卡选择不起作用。

“收藏夹”视图中的第一个单元格不会显示,我也不知道为什么。

这是它的样子: Picture of collection view with missing cell

这是我的UICollectionViewController代码:

重用IdentifierList是一个字符串列表,对应于此故事板版本中的单元格。并且我已经检查了它们的拼写是否正确。

一个失踪者是“ MyCell”单元格。

class UICollectionCollectionViewController: UICollectionViewController {
   var reuseIdentifierList = ["MyCell",
                       "MyCell2",
                       "MyCell3",
                       "MyCell4",
                       "MyCell5",
                       "MyCell6",
                       "MyCell7",
                       "MyCell8",
                       "MyCell9",
                       "MyCell10",
                       "MyCell11",
                       "MyCell12",
                       "MyCell13",
                       "MyCell14",
                       "MyCell15",
                       "MyCell16",
                       "MyCell17",
                       "MyCell18"
                       ]
override func viewDidLoad() {
    super.viewDidLoad()
     reuseIdentifier = reuseIdentifierList[0]

    self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return reuseIdentifierList.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[indexPath.item], for: indexPath)
    if indexPath.item != indexPath.startIndex {
     cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[indexPath.item], for: indexPath)

    }else{
       cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[0], for: indexPath)
    }

    print(indexPath.item)

    print(reuseIdentifierList[indexPath.item])
    // Configure the cell

    return cell
}


@IBAction func unwindToViewControllerLevel (sender: UIStoryboardSegue){

}
}

预先感谢

1 个答案:

答案 0 :(得分:0)

创建那么多单元格没有意义。只需要一个数组作为您的数据源即可。就您而言,您似乎希望您的单元格具有一个按顺序排列的数字按钮。

class UICollectionCollectionViewController: UICollectionViewController {

    // let dataSourceArray = [Int](1...20)  // [1, 2, 3 ..., 20]

    // EDIT -- SegueId
    let dataSourceArray = [(name: "1", segueId: "segueId1"), (name: "2", segueId: "segueId2"), (name: "3", segueId: "segueId3")]

    let cellId = "customCellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: cellId)
    }    

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return dataSourceArray.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    let item = dataSourceArray[indexPath.item]
    cell.button.setTitle(item.name, for: .normal)
    let segueId = item.segueId
    // use segue id to perform different segues
    return cell
}