重新加载集合视图数据时对成员“collectionView(_:numberOfItemsInSection:)"的不明确引用 [英] Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)' when reloading collection view data

查看:26
本文介绍了重新加载集合视图数据时对成员“collectionView(_:numberOfItemsInSection:)"的不明确引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In an app I've been writing, one of the tabs contains a Collection View that is originally filled with dummy information, but is supposed to be replaced once the user inputs some information in another tab. However, when I try to use self.collectionView?.reloadData() Xcode says Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)' . The code relevant to getting the data for the slots was removed. Does anyone here have any idea why it isn't working?

//
//  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 财产免费.您需要从故事板中拖入一个插座并将其命名为 @IBOutlet var collectionView: UICollectionView!.

You are using UIViewController and not UICollectionViewController so you don't get the collectionView property for free. You need to drag in an outlet form your storyboard and name it @IBOutlet var collectionView: UICollectionView!.

这篇关于重新加载集合视图数据时对成员“collectionView(_:numberOfItemsInSection:)"的不明确引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆