将静态单元格添加到UICollectionView [英] Adding a static cell to a UICollectionView

查看:257
本文介绍了将静态单元格添加到UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionView,它显示数组中的单元格。我希望第一个单元格是一个静态单元格,以提示您进入创建流程(最终添加一个新单元格)。

I have a UICollectionView that displays cells from an array. I want the first cell to be a static cell that serves as a prompt to segue into a create flow (eventually adding a new cell).

我的方法是将两个部分添加到我的collectionView中,但是目前我无法弄清楚如何在cellForItemAtIndexPath中返回单元格。这是我的尝试:

My approach would have been to add two sections to my collectionView, but I currently can't figure out how to return a cell within cellForItemAtIndexPath if I do so. This is my attempt:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if indexPath.section == 0 {
        let firstCell = collectionView.dequeueReusableCellWithReuseIdentifier("createCell", forIndexPath: indexPath) as! CreateCollectionViewCell
        firstCell.imageView.backgroundColor = UIColor(white: 0, alpha: 1)
        return firstCell
    } else if indexPath.section == 1 {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mainCell", forIndexPath: indexPath) as! MainCollectionViewCell
        cell.imageView?.image = self.imageArray[indexPath.row]
        return cell
    }
}

此问题是我必须在函数末尾返回一个单元格。它似乎不会作为if条件的一部分返回。感谢您的帮助!

The problem with this is that I have to return a cell at the end of the function. It seems that it won't be returned as part of an if condition. Thanks for helping!

推荐答案

详细讨论Dan的注释,该函数必须返回 UICollectionViewCell 。现在,编译器可以看到 indexPath.section 既不为0也不为1的代码路径。如果发生这种情况,您的代码将不返回任何内容。没关系,这永远不会在您的应用程序中逻辑上发生。

Elaborating on Dan's comment, the function must return an instance of UICollectionViewCell. At the moment the compiler can see a code path where indexPath.section is neither 0 nor 1. If this occurs, your code returns nothing. It doesn't matter that this will never occur logically in your app.

最简单的解决方法是将 else if更改为 else 。像这样:

The easiest way to fix it is to just change the "else if" to an "else". As in:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if indexPath.section == 0 {
        let firstCell = collectionView.dequeueReusableCellWithReuseIdentifier("createCell", forIndexPath: indexPath) as! CreateCollectionViewCell
        firstCell.imageView.backgroundColor = UIColor(white: 0, alpha: 1)
        return firstCell
    } else { // This means indexPath.section == 1
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mainCell", forIndexPath: indexPath) as! MainCollectionViewCell
        cell.imageView?.image = self.imageArray[indexPath.row]
        return cell
    }
}

现在,如果只有两个代码路径,并且都返回一个单元格,那么编译器会更快乐。

Now if there are only two code paths, and both return a cell, so the compiler will be happier.

这篇关于将静态单元格添加到UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆