【发布时间】:2016-07-11 02:56:29
【问题描述】:
我的故事板中有以下 segue,它连接到 ViewController,其根视图是 UICollectionView
跟随本教程:https://www.youtube.com/watch?v=L9cZrAbxN1E
我使用自定义数据源创建了一个类似的UICollectionViewController 设置。他的代码和我自己的代码之间的主要区别在于,他立即在viewDidLoad 中设置对象的 dataArray,这些对象在每个 collectionView 单元格上一对一映射,而我从异步回调中获取它们并将生成的对象设置为我的视图单元格的数据源数组。
问题是我的视图中没有添加任何内容,在我按照开头显示的 segue 进行操作之后。 Storyboard 中显示的视图的主 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 的事实(我使用打印语句进行了测试)
任何帮助表示赞赏。
供参考, 这是我用作起点的项目 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:
【问题讨论】:
标签: ios iphone swift uicollectionview