0

我有一个奇怪的问题collectionView,我有两个原型,第一个应该绘制,indexPath.row=0第二个应该创建四次。第二个原型包含一个 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()

}

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

4

2 回答 2

0

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

  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
}
于 2019-05-23T00:09:26.757 回答
0

I think you should work on [collectionViewLayout] delegation method.

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    // Specify the cell frame of row=1
    if indexPath.row == 1 {
        return CGSize(width: collectionView.bounds.width - 20, height: collectionView.bounds.height/4-5)
    }

    // Other...
    return CGSize(width: (collectionView.bounds.width-40)/3 - 1, height: collectionView.bounds.width/3-5)

}

于 2019-05-23T00:17:21.197 回答