UICollectionView 将第一个单元格设置为始终是特定内容

UICollectionView setting first cell to always be specific content

您好,我正在使用横​​向滚动 UICollectionView 来显示用户创建的人群。这些组存储在我的服务器上,当视图加载时,它们从服务器加载。但是我希望第一个单元格始终相同,这是一个允许您创建组的单元格。这是我需要的布局。

我知道如何使用多个不同的自定义单元格,但如何才能使第一个单元格是静态的,而从我的服务器加载内容后的单元格呢?谢谢:)

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

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

    if indexPath.row == 0 {
    let cell : AddGroupCollectionViewCell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("Add", forIndexPath: indexPath) as! AddGroupCollectionViewCell

    return cell

    } else {

    let cell : FriendGroupsCell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FriendGroupsCell


    cell.groupImage.image = UIImage(named: "pp")
    cell.groupNameLabel.text = familyName[indexPath.row]

    return cell
    }
}

这是我的代码,它遗漏了数组中的第一个人,因为索引路径跳过了它。我该如何修改它才能正常工作

UICollectionViewCell 正在利用重用技术来提高性能。记住这一点。单元格中的任何内容都不能是静态的,因为此单元格稍后将位于另一个索引上。

您可以使用 collectionView:cellForItemAtIndexPath: 通过 indexPath.row == 0

使第一个单元格始终加载相同的 images/labels

您可以使用prepareReuse方法清理单元格中的资源。因此,如果 2 号单元格将成为新的 1 号单元格,它就有机会清理旧资源。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : AddGroupCollectionViewCell = collectionViewOutlet.dequeueReusableCellWithReuseIdentifier("Add", forIndexPath: indexPath) as! AddGroupCollectionViewCell
    if indexPath.row == 0 {
        cell.groupImage.image = UIImage(named: "new")
        cell.groupNameLabel.text = "new"
    } else {
        cell.groupImage.image = UIImage(named: "pp")
        cell.groupNameLabel.text = familyName[indexPath.row]
    }
    return cell
}