我该如何使用withReuseIdentifier

时间:2017-09-22 10:25:03

标签: ios swift xcode uicollectionview uicollectionviewcell

如何使用多个--nofetch

这是代码 因为我有4个按钮不起作用 当我使用标识符“1”时,其他按钮不会被忽略

这是代码

cordova plugin add path:\to\my-plugin --nofetch

新代码

withReuseIdentifier

和此代码

extension ViewController : UICollectionViewDataSource
{
func numberOfSections(in collectionView: UICollectionView) -> Int
{
    return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return interests3.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as! interestCollectionViewCell
    cell.interest2 = self.interests3[indexPath.item]
    return cell
}

1 个答案:

答案 0 :(得分:0)

试试这个,在你的viewDidLoad方法中添加

self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "1")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "2")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "3")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "4")

并更改collectionView cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell
        } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "2", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell

        } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "3", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell

        } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "4", for: indexPath as IndexPath) as? interestCollectionViewCell
        {
            cell.interest2 = self.interests3[indexPath.item]
            return cell
        }
     return interestCollectionViewCell()
}