gpt4 book ai didi

ios - 在移动应用程序上创建新闻提要的 UICollectionView 代码

转载 作者:行者123 更新时间:2023-11-28 18:28:44 24 4
gpt4 key购买 nike

我是编码初学者,我想学习设计 iOS 移动应用程序。我正在通过观看 Youtube 重新创建已经构建的应用程序来学习。我目前正在重新创建 Facebook 新闻提要。下面是我学到的自定义代码。我一直停留在我们构建单元格及其大小的部分。具体来说,我收到以下错误:collectionView?.registerClass(FeedCell.self, forCellWithReuseIdentifier: cellId)。看起来这在以前的 Swift 版本中是可行的,但对于 3.0 就不行了。感谢您的帮助!

代码如下:

import UIKit

let cellId = "cellId"

class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.title = "Facebook Feed"


collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)

collectionView?.registerClass(FeedCell.self, forCellWithReuseIdentifier: cellId)


}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
}

func collectionView(_collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 50)



}
}

class FeedCell: UICollectionViewCell {

override init(frame: CGRect){
super.init(frame: frame)

setupViews()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func setupViews() {

backgroundColor = UIColor.white

最佳答案

当您创建自定义 collectionView 单元格时,您必须使用“collectionView.registerNib”而不是 collectionView.registerClass。

collectionView.registerNib(UINib(nibName: "Your Nib Name", bundle: nil), forCellWithReuseIdentifier: "Your Cell Identifier")

关于ios - 在移动应用程序上创建新闻提要的 UICollectionView 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42187422/

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