ios - 不知道为什么单元格没有出现并且没有错误
问题描述
我通过代码而不是自动布局编写设计,因此当我更改集合视图的背景颜色时,我用一个单元格绘制集合视图,它改变了但单元格没有出现。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewLayout()
window?.rootViewController = UINavigationController(rootViewController: ViewController(collectionViewLayout: layout) )
return true
}
import UIKit
class ViewController:UICollectionViewController {
var cell : UICollectionViewCell?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
collectionView?.backgroundColor = UIColor.white
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
cell?.backgroundColor = UIColor.red
return cell!
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
}
}
没有错误,但单元格没有出现。
解决方案
使用UICollectionViewLayout
需要您手动管理布局属性,这可能需要一些工作,并且需要稍微深入了解幕后UICollectionView
工作原理。
实现您想要做的最简单的方法是将布局更改为UICollectionViewFlowLayout
在您的 AppDelegate 中,更改
let layout = UICollectionViewLayout()
至
let layout = UICollectionViewFlowLayout()
我可能还建议通过删除以下代码行来避免在控制器中捕获对单元格的引用:
var cell : UICollectionViewCell?
因此,完全修改您的代码将导致:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: ViewController(collectionViewLayout: layout) )
return true
}
}
class ViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
collectionView?.backgroundColor = UIColor.white
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
}
推荐阅读
- firebase - 如何动态地将一组地图添加到云火库中?
- r - dplyr 包中的不同功能
- html - 如何在html中的链接中嵌入源站地址?
- azure - Azure - 创建多个具有静态地址的虚拟机 - 冲突
- c# - 如何从 Xamarin 表单中的其他页面动态更改 TabbedPage 的 backgroundColor
- sql-server - 如何使主键成为另一个表中的主键和 Microsoft SQL Server 中的 2 个字母的组合
- node.js - 打字稿编译错误 - TS2769:没有重载匹配此调用
- php - 在 PHP 中的控制器中包含模型函数
- r - 如果记录与其他数据框重复,则改变数据框
- python - JSON查询:python中的日期时间验证