简体   繁体   English

将数据传递给collectionView(numberOfItemsInSection)函数(Swift)

[英]Passing Data to collectionView(numberOfItemsInSection) function (Swift)

I'm having trouble passing data from my model to a collection view. 我无法将数据从模型传递到集合视图。 I've simplified my code to a simple project to illustrate the problem, which is available here: https://github.com/routineCode/collectionViewDataProblem 我已经将代码简化为一个简单的项目来说明问题,可以在这里找到: https : //github.com/routineCode/collectionViewDataProblem

Here is an image showing the Desired Result vs. Actual Result with the current code: View Controller output showing Desired vs. Actual Result 这是使用当前代码显示所需结果与实际结果的图像: 查看控制器输出,显示所需结果与实际结果

I want the collectionView(numberOfItemsInSection) function to return the number of items (5) in my [data] array but it instead returns the hard-coded value (3). 我希望collectionView(numberOfItemsInSection)函数返回我的[data]数组中的项目数(5),但它返回的是硬编码值(3)。 Through debugging, I realize that this is because viewController is always nil when this function is called, but I don't understand how to get around the problem. 通过调试,我意识到这是因为在调用此函数时viewController始终为nil,但是我不知道如何解决该问题。 I also make use of the [data] array in the collectionView(cellForItemAt) function, and that works fine because viewController is not nil by the time that function is called. 我还使用了collectionView(cellForItemAt)函数中的[data]数组,它可以正常工作,因为在调用该函数时viewController并不为零。 Thanks for any help. 谢谢你的帮助。

Try to reloadData after you set the ViewController 设置ViewController后尝试reloadData

lazy var menuBar: MenuBar = {
    let mb = MenuBar()
    mb.viewController = self
    mb.cView.reloadData()
    return mb
  }()

By the way, your code has problem with memory leak 顺便说一句,您的代码有内存泄漏问题

Retain circle 保留圈

Retain circle is when two objects has strong reference to each other, so no one never deallocates. 保留圈是两个对象之间具有很强的参照关系,因此没有人永不释放。 Here is an example of retain circles that is right exactly the same as yours. 是一个与您的保留圆完全相同的保留圆的示例。

In your Code the ViewController has a strong reference to menuBar 在您的代码中,ViewController对menuBar有很强的引用

lazy var menuBar: MenuBar ...

And MenuBar has a strong reference to ViewController 而且MenuBar对ViewController有很强的引用

var viewController: ViewController?

You can set the viewController property to weak, but what for you give the whole ViewController? 您可以将viewController属性设置为weak,但是为​​整个ViewController提供什么呢? Why your menuBar should know about the whole viewController? 为什么您的menuBar应该了解整个viewController? You can just remove the viewController property and give it a data property instead. 您可以只删除viewController属性,然后给它一个data属性。

let data = [String]()

And set it in your viewController 并在您的viewController中进行设置

lazy var menuBar: MenuBar = {
    let mb = MenuBar()
    mb.data = data
    return mb
  }()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 collectionView:numberOfItemsInSection: 在 UITableviewcell (DetailedCell) - collectionView:numberOfItemsInSection: in UITableviewcell (DetailedCell) UITableviewCell中的collectionView numberOfItemsInSection - collectionView numberOfItemsInSection in UITableviewCell 重新加载集合视图数据时对成员“collectionView(_:numberOfItemsInSection:)”的不明确引用 - Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)' when reloading collection view data 在collectionView numberOfItemsInSection中无法识别UICollectionView: - UICollectionView not recognized in collectionView numberOfItemsInSection: 将数据从 Tableview 传递到 CollectionView SWIFT - passing data from Tableview to CollectionView SWIFT Swift-将数据从CollectionView传递到TableViewController - Swift - Passing data from a CollectionView to a TableViewController CollectionView numberOfItemsInSection变量 - CollectionView numberOfItemsInSection variable 在collectionView:numberOfItemsInSection中传递的UICollectionView为nil - UICollectionView passed in collectionView:numberOfItemsInSection: is nil 具有大量数据的CollectionView Swift - CollectionView with lots of data Swift [UIView collectionView:numberOfItemsInSection:]: 无法识别的选择器发送到实例 - [UIView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM