UICollectionView以编程方式
我在故事板中创建了自己的窗口,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() window?.rootViewController = UINavigationController(rootViewController: HomeController()) return true }
当我的类inheritanceUIViewController时,这实际上工作正常。
class HomeController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. view.backgroundColor = UIColor.red } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
现在,当我用UICollectionViewController子类化我的类时:
class HomeController: UICollectionViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. Collectionview.backgroundColor = UIColor.red } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
在运行时它给我一个错误说:
由于未捕获的exception’NSInvalidArgumentException’而终止应用程序,原因:’UICollectionView必须使用非零布局参数初始化’***首先抛出调用堆栈:
根据我的理解,它要求我为UICollectionView提供布局。如果我错了,请在这里告诉我。所以我试过这个 – :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() let layout = UICollectionViewFlowLayout() window?.rootViewController = UINavigationController(rootViewController: HomeController(UICollectionViewLayout: layout)) return true }
但仍然以错误结束:
参数标签’UICollectionViewLayout’与任何可用的重载都不匹配。
我使用了错误的swift3语法吗? 如果是,解决这个问题是正确的。
您需要使用UICollectionViewController
的指定intialiser,如下所示。
let layout = UICollectionViewLayout() let homeViewController = HomeController(collectionViewLayout: layout) window?.rootViewController = UINavigationController(rootViewController: homeViewController)
除此之外,您还需要修改HomeViewController
以实现UICollectionViewDataSource
所需的方法。
override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Register cell classes self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") // Do any additional setup after loading the view. } // MARK: UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections return 0 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return 0 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) // Configure the cell return cell }
您需要在UICollectionViewFlowLayout
中实现UICollectionViewController
。
检查此链接
您必须使用initWithFrame:collectionViewLayout:
初始化您的UICollectionView
,您必须传递一个非零的UICollectionViewLayout object
。
- 将iOS Core Data添加到现有项目 – 解包可选值时发生错误
- dynamicUITableViewCell内容不会展开单元格
- 如何为UIScrollView启用方向锁?
- Xcode 8中的UITabbarController在故事板视图中显示一个蓝色矩形
- 在iOS上的多个应用程序共享数据
- 为什么页面推动animationTabbar在iPhone X中向上移动
- 如何使用NSUserDefaults在Swift中存储自定义类的数组?
- 在使用UIImagePickerControllerOriginalImage从UIImagePickerController获取图像时获取内存警告
- 如何调整iOS中的图像?