ios 集合视图单元格没有出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33881683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:17:05  来源:igfitidea点击:

Collection View Cells not appearing

iosxcodeswiftuicollectionviewcellcollectionview

提问by maidi

I want to display as many collectionViewCellswith buttonsas there are strings in my array. but when I start the simulator there is just the background of the CollectionViewbut no cells shown. What could be the error?

我想显示collectionViewCellsbuttons数组中的字符串一样多的字符串。但是当我启动模拟器时,只有背景CollectionView但没有显示单元格。可能是什么错误?

Here is the code from my CollectionViewControllerthat I attached to the CollectionViewin the main.storyboard:

这是CollectionViewController我附加到CollectionViewmain.storyboard 中的代码:

class CollectionViewController: UICollectionViewController {

var Array = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    Array = ["1","2","3","4","5"]
}

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

func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int) -> Int {
    return Array.count
}

override func
    collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell

        var button = cell.viewWithTag(1) as! UIButton
        button.titleLabel?.text = Array[indexPath.row]

        return cell
}

}

}

These are the connections of the Collection View Controller:

这些是集合视图控制器的连接:

connections

连接

The View Controller on the Storyboard:

故事板上的视图控制器:

viewcontroller

视图控制器

回答by Pavel Smejkal

Did you set the CollectionViewControllerto the storyboard identity inspector? :)

您是否将 设置CollectionViewController为故事板身份检查器?:)

And I would try to call the reloadData()after you change the data in the viewDidLoadmethod.

我会尝试reloadData()在您更改viewDidLoad方法中的数据后调用。

Hope that helps

希望有帮助

回答by Subash

func layoutCells() {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        layout.minimumInteritemSpacing = 5.0
        layout.minimumLineSpacing = 5.0
        layout.itemSize = CGSize(width: (UIScreen.mainScreen().bounds.size.width - 40)/3, height: ((UIScreen.mainScreen().bounds.size.width - 40)/3))
        collectionView!.collectionViewLayout = layout
    }

Try this. Call this function from view did load. I think the problem is that your collection is not laid out correctly.

尝试这个。从视图中调用此函数确实加载。我认为问题在于您的收藏布局不正确。

func viewDidLoad() {
    layoutCells()
}

If this works you can modify the layout options to meet your needs.

如果可行,您可以修改布局选项以满足您的需要。

回答by Bhavin Kansagara

Problem is with setting the title to button, use the following code.

问题是将标题设置为按钮,请使用以下代码。

override func
    collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell

        var button = cell.viewWithTag(1) as! UIButton
        //button.titleLabel?.text = Array[indexPath.row]
        button.setTitle(Array[indexPath.row], forState: UIControlState.Normal)
        return cell
}

Hope it helps

希望能帮助到你

回答by Ges83

I had a similar problem, I found this that was somehow restraining the size of my cell. Hope this helps.

我有一个类似的问题,我发现这以某种方式限制了我的单元格的大小。希望这可以帮助。

Select the CollectionViewCell

选择 CollectionViewCell

Find this values in size inspector

Find this values in size inspector

回答by Hamid Reza Ansari

check that you are setting CollectionView outlets (datasource , delegate) in your storyBoard as show below

检查您是否在您的故事板中设置 CollectionView 插座(数据源,委托),如下所示

helpful image link

helpful image link

回答by Subash

Make sure you have the correct reuse identifier set for the cell.

确保为单元设置了正确的重用标识符。

enter image description here

enter image description here

回答by Kannan Chandrasegaran

Ideas:

想法:

  • Check if your cell reuse identifier is set up.
  • Check if you've set the collection view subclass correctly in the storyboard.
  • Put printstatements inside your cellForItemAtIndexPathfunction to see if it's being called ever.
  • Not sure if this should be an issue, but Arrayis actually a type in Swift. Using that as your variable name might be messing with the logic somehow. Rename it to array(lowercase). I believe this is best practice for variable names anyway.
  • Do something else to the cell in cellForItemAtIndexPath, such as changing the background color. If that works, maybe there's just something wrong with what you're doing with tags.
  • 检查您的单元重用标识符是否已设置。
  • 检查您是否在故事板中正确设置了集合视图子类。
  • print语句放入您的cellForItemAtIndexPath函数中以查看它是否被调用过。
  • 不确定这是否应该是一个问题,但Array实际上是 Swift 中的一种类型。使用它作为你的变量名可能会以某种方式扰乱逻辑。将其重命名为array(小写)。我相信无论如何这是变量名的最佳实践。
  • 对 中的单元格执行其他操作cellForItemAtIndexPath,例如更改背景颜色。如果这样做有效,那么您使用标签的操作可能有问题。

回答by rkyr

Your problem is in

你的问题在

func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int)

func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int)

method signature. It is wrong. It should be

方法签名。这是错误的。它应该是

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

and with overridespecification (because UICollectionViewControllerhas own, this why you get empty collection).

override规范(因为UICollectionViewController有自己的,这就是为什么你得到空集合)。

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  return Array.count
}

回答by Rushang Prajapati

First of all check you have used this method:-

首先检查您是否使用过此方法:-

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return searches.count
}

There's one search per section, so the number of sections is the count of the searches array. so also we can use as per needed this method.by default we can use retuen 1

每个部分有一个搜索,因此部分的数量是搜索数组的计数。所以我们也可以根据需要使用这个方法。默认我们可以使用 retuen 1

And Follow step in the Link for better soluation Check this link

并按照链接中的步骤以获得更好的解决方案 检查此链接

回答by William Hu

Select the UIButtonin storyboard, check the installedas the bottom left in the image below.

选择UIButton情节提要,检查installed下图中左下角的。

enter image description here

enter image description here