【问题标题】:Deque custom cells from specific indexpath SWIFT从特定的 indexpath SWIFT 中对自定义单元进行 Deque
【发布时间】:2018-03-27 21:17:26
【问题描述】:

我有 UICollectionView,我正在下载图像并在单元格中显示它们。我的第一个单元格是屏幕宽度并包含一个按钮,其余的是一般单元格。该应用程序仅对前 2 个单元进行出队,应该有 3 个单元。

我的cellForItemAtIndexPath 功能:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.row == 0 {
        print("yay")
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UploadNewCell", for: indexPath) as! UploadNewCell
        return cell
    }else if indexPath.row > 0 {
        let userImages = userposts[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCell", for: indexPath) as! ProfileCell
            cell.fillCells(uid: uid!, userPost: userImages)
            return cell
    }else{
        return ProfileCell()
    }
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if indexPath.row == 0 {
        print("hellowold")
    } else {
        let selecteditem : String!
        selecteditem = userposts[indexPath.row]
        performSegue(withIdentifier: "lol", sender: selecteditem)
    }
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}


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


}

我的看法:

单元格中应该有 3 个图像,其中一个在第一个索引中被 dequed。

我没有想法,关于解决方案有什么想法吗?

【问题讨论】:

  • "当我选择第一个索引上的单元格时,它没有按预期响应。"那个代码在哪里?
  • @Larme 更新问题和查询。单元格选择现在表现良好。将单元格出队是一个问题。再看问题。抱歉问错了。
  • 部分返回的项目数...?
  • 抱歉,我的机器出现问题。代码现已更新。感谢您的包容。
  • 返回 userpost.count+1 in section 中的项目数

标签: ios swift3 uicollectionview uicollectionviewcell


【解决方案1】:

let userImages = userposts[indexPath.row]

此时,在您的代码中,indexPath.row > 0。
数组是从 0 开始的,因此第一个单元格 (indexPath.row == 1) 将获取数组中的第二项 (user posts[1]),这是您想要的第二个图像。

我能想到几个简单的改变:

  • 更改您正在访问的索引,例如:
    let userImages = userposts[indexPath.row - 1]
  • numberOfItemsInSection:userposts.count 值上加1
  • 将你的collectionView分成多个部分,所以顶部单元格(UploadNewCell)是部分0,底部三个ProfileCells是第二部分:这允许您检查indexPath.section,并直接分配从行:
    let userImages = userposts[indexPath.row]

注意:我实际上建议进一步修改第二个选项的代码,为SectionType 创建一个枚举。这允许您对潜在值执行switch,从而避免这种讨厌的默认实现,并提高代码的可读性。

【讨论】:

  • 非常感谢。我只是在索引中添加 +1。我的错。这是恰当的答案。感谢您的及时回复。干杯
  • 另外,我只是在尝试部分。我也认为,Enum 将是未来消除严重错误的更好选择
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
相关资源
最近更新 更多