gpt4 book ai didi

swift - UICollectionView 没有被渲染

转载 作者:行者123 更新时间:2023-11-28 06:03:14 26 4
gpt4 key购买 nike

此代码启动了 UICollectionView 并分配了数据源。单元格应该被渲染但是单元格没有被渲染。在这种情况下,DataSource 被提取出来。

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 中时,这段代码会渲染单元格

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 中实例化的东西被遗漏了?

最佳答案

在第一个片段中,您在 viewDidLoad() 中实例化了数据源,

let dataSource = DateSource()

这会在该方法的范围内限制数据源对象的生命周期。因此,您需要将数据源声明为 View Controller 的成员;

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)
}
...
..
}

第二个代码段有效,因为数据源是自身的,并且它的范围将比 collectionView 的范围长。

关于swift - UICollectionView 没有被渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48965852/

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