我的故事板中有以下segue,它连接到ViewController,其根视图是UICollectionView
遵循本教程:https://www.youtube.com/watch?v=L9cZrAbxN1E
我使用自定义dataSource创建了UICollectionViewController
的类似设置。他的代码与我自己的代码之间的主要区别在于,他立即设置了对象的dataArray,这些对象在每个collectionView单元格中一对一地映射到viewDidLoad
,而我从异步回调中抓取它们并设置结果我的dataSource数组中的对象用于我的视图单元格。
问题是,在我按照开头显示的segue后,我的视图中没有添加任何内容。故事板中描绘的视图的主ViewController类型为FeedController
,所以我知道这不是原因。我觉得这是因为我的contentSize可能没有正确设置。这很奇怪,因为根据我的知识,视频中的人从未明确地设置了他的collectionView的大小。
import UIKit
let cellId = "cellId"
class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var recipes = [BrowsableRecipe]()
var recipeIndex = 1
var pageSize = 10
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Homepage"
collectionView?.alwaysBounceVertical = true
collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)
collectionView?.registerClass(RecipeCell.self, forCellWithReuseIdentifier: cellId)
ServerMessenger.sharedInstance.getRecipesForHomePage(recipeIndex, pageSize: pageSize){
responseObject, error in
if let data = responseObject{
self.recipes = data
print(self.recipes)
}
}
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return recipes.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let recipeCell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! RecipeCell
print("cellForItem called")
recipeCell.recipe = recipes[indexPath.item]
return recipeCell
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
collectionView?.collectionViewLayout.invalidateLayout()
}
}
class RecipeCell: UICollectionViewCell {
var recipe: BrowsableRecipe? {
didSet {
if let name = recipe?.recipeName {
let attributedText = NSMutableAttributedString(string: name, attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(14)])
nameLabel.textColor = UIColor.blackColor()
print("in did set")
nameLabel.attributedText = attributedText
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let nameLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 2
return label
}()
func setupViews() {
backgroundColor = UIColor.blueColor()
addSubview(nameLabel)
addConstraintsWithFormat("V:|-12-[v0]", views: nameLabel)
}
}
extension UIColor {
static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}
extension UIView {
func addConstraintsWithFormat(format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerate() {
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
}
我确信这个collectionView
中没有显示任何内容的原因直接源于cellForItemAtIndexPath
永远不会被调用的事实(我使用print语句进行了测试)
任何帮助赞赏。
供参考, 这是我用作起点的项目 https://github.com/purelyswift/facebook_feed_dynamic_cell_content
更新:
我尝试在异步回调中使用reloadItemsAtIndexPaths
执行此操作:
我得到了
reason: 'attempt to delete item 9 from section 0 which only contains 0 items before the update'
这表明我需要先在第0部分添加内容。所以我试试:
self.collectionView!.insertItemsAtIndexPaths(myArrayOfIndexPaths)
我得到了:
reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath:
答案 0 :(得分:1)
设置数据阵列后调用重新加载数据。在设置数据数组之前,Coz系统调用realod将调用
if let data = responseObject{
self.recipes = data
print(self.recipes)
dispatch_async(dispatch_get_main_queue()) { // if you are setting in different thread
collectionView.reloadData()
}
}
如果不检查它是否正在调用'numberOfItemsInSection'方法。如果不是,你可能没有设置dataSource和委托。