ios - UIView 类中的 Swift Collection View 委托(delegate)
<p><p>我通过 UIView 内的 Storyboard有一个 Collection View 。我正在继承 UIView 并调用一个名为 setupView() 的方法。当我尝试从类中分配代表和数据源时,它似乎不起作用。我可以通过这个类分配委托(delegate)和数据源还是必须通过 Controller 来完成?</p>
<p>这是我的代码:</p>
<pre><code>class homeView: UIView, UITextFieldDelegate {
@IBOutlet weak var collectionView: UICollectionView!
let storedService = StoredService()
override init(frame: CGRect) {
super.init(frame: frame)
print("I was called") // This doesn't print out!
setupView()
}
fileprivate func setupView() {
collectionView.dataSource = storedService
collectionView.delegate = storedService
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>当然。它确实可以通过 UIView 类来完成。但它不会在 <code>init(frame: CGRect)</code> 方法中工作。您需要在 <code>layoutSubviews()</code> 或 <code>draw(_ rect: CGRect)</code> </p> 中运行它
<p>像这样:</p>
<pre><code>override func layoutSubviews() {
collectionView.delegate = self
collectionView.dataSource = self
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIView 类中的 SwiftCollection View 委托(delegate),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44821434/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44821434/
</a>
</p>
页:
[1]