UICollectionView 未呈现 [英] UICollectionView not getting rendered
问题描述
此代码已启动 UICollectionView 并分配数据源.单元格应该被渲染,但是单元格没有被渲染.在这种情况下,数据源被提取出来.
This code has UICollectionView getting initiated and datasource being assigned. Cells are supposed to be rendered however the cells are not getting rendered. The DataSource is extracted out in this case.
import UIKit
class ViewController: UIViewController {
private let cellReuseIdentifier = "collectionCell"
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
let dataSource = DateSource()
collectionView.dataSource = dataSource
collectionView.backgroundColor = UIColor.cyan
self.view.addSubview(collectionView)
}
}
class DateSource:NSObject, UICollectionViewDataSource {
let leftAndRightPaddings: CGFloat = 80.0
let numberOfItemsPerRow: CGFloat = 7.0
let screenSize: CGRect = UIScreen.main.bounds
private let cellReuseIdentifier = "collectionCell"
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath)
cell.backgroundColor = UIColor.green
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let width = (screenSize.width-leftAndRightPaddings)/numberOfItemsPerRow
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 20, left: 8, bottom: 5, right: 8)
}
}
但是当数据源在 ViewController 中合并时,此代码会呈现单元格
But this code renders the cells, when the datasource is sort of merged in the ViewController
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource {
private let cellReuseIdentifier = "collectionCell"
let leftAndRightPaddings: CGFloat = 80.0
let numberOfItemsPerRow: CGFloat = 7.0
let screenSize: CGRect = UIScreen.main.bounds
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
let dataSource = self
collectionView.dataSource = dataSource
collectionView.backgroundColor = UIColor.cyan
self.view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath)
cell.backgroundColor = UIColor.green
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let width = (screenSize.width-leftAndRightPaddings)/numberOfItemsPerRow
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 20, left: 8, bottom: 5, right: 8)
}
}
我在这里有点迷茫,是不是要在 DataSource 中实例化一些东西,但遗漏了什么?
I am a little lost here, was something to be instantiated inside DataSource, which was missed ?
推荐答案
在第一个片段中,您已经在 viewDidLoad()
中实例化了数据源,
In the first snippet, you've instantiated datasource in viewDidLoad()
,
let dataSource = DateSource()
这会在该方法的范围内限制数据源对象的生命周期.您需要将您的数据源声明为您的视图控制器的成员;
This restricts the lifespan of your datasource object within the scope of that method. You need to declare your datasource as a member of your View Controller, as such;
import UIKit
class ViewController: UIViewController {
let dataSource = DateSource()
private let cellReuseIdentifier = "collectionCell"
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
collectionView.dataSource = dataSource
collectionView.backgroundColor = UIColor.cyan
self.view.addSubview(collectionView)
}
...
..
}
第二个代码段有效,因为数据源是 self 并且它的范围将超过 collectionView 的范围.
Second snippet works since the datasource is self and it's scope will outlive the collectionView's.
这篇关于UICollectionView 未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!