【问题标题】:UICollectionView not appearing in simulatorUICollectionView 没有出现在模拟器中
【发布时间】:2014-08-29 17:48:42
【问题描述】:

当我构建我的项目时,屏幕在模拟器中只是显示为白色(其中集合视图为黑色),当然也没有我想要显示的单元格。

我正在使用 xcode6-beta6,这是用于 ios (ipad)。

所以我试图用标签填充集合视图。在我的故事板中,我有一个视图控制器,里面只有一个集合视图,里面有一个单元格,我在里面放了一个标签。我知道我在故事板和我的视图控制器代码中正确设置了标识符和标签。我知道我也正确设置了我的数据源和委托出口。我一直在关注本教程https://www.youtube.com/watch?v=jiiN9oFH3vE,但是它在 xcode 5 和 Objective-c 中,所以我不知道在 swift/xcode6 中是否有一些我没有做的完全不同的东西。

这是我的代码

import UIKit

class FirstViewController: UIViewController,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var testArray:[String]!

override func viewDidLoad() {
    super.viewDidLoad()
    testArray = ["hello","world","hola","mmg","me","la"]
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int{
    return 1
}


func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int{
        return testArray.count
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as FirstCollectionViewCell

    var label = cell.viewWithTag(1) as UILabel
    label.text = testArray[indexPath.row]
    return cell
}



}

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell xcode6-beta6


    【解决方案1】:

    您是否在任何地方将UICollectionView 委托和数据源设置为self?我在您的代码中没有看到它,这可能是问题所在。您可能在 viewDidLoad 方法中需要它。

    【讨论】:

    • 对不起,我不关注。设置为自己?我没有遇到过这个,我认为在任何教程中都没有。
    • 类似 self.collectionView.delegate = self;
    • 我收到错误“(UICollectionView, numberOfItemsInSection:Int)->Int 没有名为 'delegate' 的成员”我也收到了 self.collectionView.dataSource = self 的类似错误
    • 你应该在 viewdidload 中这样做
    • 是的,我是。此外,xcode 没有为我提供自动填充功能,这让我怀疑这仍然是 swift 的事情吗?也许需要不同的语法?我在谷歌上搜索它。
    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多