gpt4 book ai didi

ios - 未调用 UICollectionViewDelegate 和 UICollectionViewDataSource 方法

转载 作者:行者123 更新时间:2023-11-28 15:55:40 25 4
gpt4 key购买 nike

我正在使用内部有一个 UICollectionViewUITableViewCell。使用这种结构,一切都完美无缺:

class PostCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {}

但是,当我开始将 UICollectionViewDelegate 和 UICollectionViewDataSource 提取到另一个类时

class PostCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!

public func populate(users: [User]) {
UsersManager(users: users, collectionView: collectionView)
}
}
public class UsersManager: NSObject, UICollectionViewDelegate, UICollectionViewDataSource {
private var users: [User]!
private var collectionView: UICollectionView!

init(users: [User], collectionView: UICollectionView) {
super.init()

let nib = UINib(nibName: UserCell.IDENTIFIER, bundle:nil)
collectionView.register(nib, forCellWithReuseIdentifier: UserCell.IDENTIFIER)
collectionView.delegate = self
collectionView.dataSource = self

collectionView.reloadData()
}
}

不调用委托(delegate)方法。

collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
numberOfSections(in collectionView: UICollectionView) -> Int

我正在将 collectionView 传递给 UsersManager()。我同时调用:

collectionView.delegate = self
collectionView.dataSource = self

UsersManager()

您认为这里的问题是什么?

最佳答案

class PostCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!

var usersManager:UsersManager? // add this line.

public func populate(users: [User]) {
usersManager = UsersManager(users: users, collectionView: collectionView)
}
}

关于ios - 未调用 UICollectionViewDelegate 和 UICollectionViewDataSource 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com