ios - 重新加载 Collection View 数据时对成员 'collectionView(_:numberOfItemsInSection:)' 的引用不明确

标签 ios swift xcode uicollectionview

在我编写的一个应用程序中,其中一个选项卡包含一个 Collection View,它最初填充了虚拟信息,但一旦用户在另一个选项卡中输入了一些信息,它就会被替换。但是,当我尝试使用 self.collectionView?.reloadData() 时,Xcode 显示 Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)' 。删除了与获取插槽数据相关的代码。这里有人知道为什么它不起作用吗?

//
//  FirstViewController.swift
import UIKit
import Alamofire

class FirstViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    self.collectionView?.reloadData()

}

// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor.white
    cell.layer.borderColor = UIColor.white.cgColor
    cell.layer.borderWidth = 5
    cell.layer.cornerRadius = 8// make cell more visible in our example project

    return cell
}

// MARK: - UICollectionViewDelegate protocol

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}

最佳答案

您使用的是 UIViewController 而不是 UICollectionViewController 因此您不会免费获得 collectionView 属性。您需要从 Storyboard 中拖入一个导出并将其命名为@IBOutlet var collectionView: UICollectionView!

关于ios - 重新加载 Collection View 数据时对成员 'collectionView(_:numberOfItemsInSection:)' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323153/

相关文章:

ios - 如何自定义单元格 View 并使它们以编程方式快速水平滚动

swift - TableViewController 的第二个 segue 创建与第一个相关的 SIGABRT 错误

ios - 如何在 Swift 中更改 UIPickerView 中的字体大小?

swift - 什么是 Swift 4 的良好实践 'Error Handling'?

iphone - 如何释放单例?

ios - Safari - iPhone 模拟器调试(通过 "develop")不再显示

ios - 如何在没有 Google Drive 的情况下直接从 Google Drive 下载大文件无法扫描此文件是否有病毒消息

ios - 部署后向 App-Info.plist 添加值

php - 查询数据库来自

ios - 5 天后提示消息的 iRate 库