ios - 在 UICollectionView 中设置排序属性的问题

标签 ios swift uicollectionview uicollectionviewcell indexpath

我对collectionView有一个奇怪的问题,我有两个原型(prototype),第一个应该在indexPath.row=0中绘制,第二个应该创建四次。 第二个原型(prototype)包含一个 UIView,用于保存使用 PaintCode 应用程序创建的图标和一个文本标签。每个单元格还有四个图标。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.row {
case 0:
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as? ViewData {
        return cell
    }
case 1,2,3,4:
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as? ViewData {
        switch indexPath.row {
        case 1:
            cell.dataIcon.is1stIcon = true
            cell.title.text = "1"
        case 2:
            cell.dataIcon.is2ndIcon = true
            cell.title.text = "2"
        case 3:
            cell.dataIcon.is3rdIcon = true
            cell.title.text = "3"
        case 4:
            cell.dataIcon.is4thIcon = true
            cell.title.text = "4"
        default:
            break
        }

        return cell
    }
default:
    break
}
return UICollectionViewCell()

}

问题是,所有文本都位于正确的位置(行),但 indexPath.row=1 中的 1stIcon 将显示在 中indexPath.row=33rtIcon 将显示在 indexPath.row=1 中。 首先,我认为问题可能出在图标上,但无论我在这些行中放入什么,都具有相同的错误倾向。 你对此有何看法? 谢谢

最佳答案

问题发生在单元出队时,您需要在使用它之前重新设置所有内容

  cell.reset()
    switch indexPath.row {
    case 1:
        cell.dataIcon.is1stIcon = true
        cell.title.text = "1"
    case 2:
        cell.dataIcon.is2ndIcon = true
        cell.title.text = "2"
    case 3:
        cell.dataIcon.is3rdIcon = true
        cell.title.text = "3"
    case 4:
        cell.dataIcon.is4thIcon = true
        cell.title.text = "4"
    default:
        break
  }

func reset() {
  // show all of them
  self.dataIcon.is1ndIcon = false
  self.dataIcon.is2ndIcon = false
  self.dataIcon.is3ndIcon = false
  self.dataIcon.is4ndIcon = false
}

关于ios - 在 UICollectionView 中设置排序属性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56266467/

相关文章:

ios - 批量更新期间重复的单元格动画

ios - 客户端向我发送了分发证书和登录用户名密码到开发门户,但没有私钥

ios - 快速将一个特定的 View Controller 设置为横向模式

ios - ios AppDelegate.swift 上的 simple_auth_flutter 导入更改无法启动

Swift CollectionView 部分类别

ios - 来自 json 的响应字节数组

ios - 如果我的应用程序处于终止状态,iOS 区域监控是否会在我的应用程序上调用 didEnterRegion?

ios - 从 UITableViewCell.subviews Swift 访问 "SeparatorView"

ios - UICollectionView 删除空格/标题 iOS11

ios - 根据collectionView.tag定义collectionView