【问题标题】:Problem with set ordering property In UICollectionView在 UICollectionView 中设置排序属性的问题
【发布时间】:2019-05-23 00:05:19
【问题描述】:

我对@9​​87654321@ 有一个奇怪的问题,我有两个原型,第一个应该在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()

}

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

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell indexpath


    【解决方案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
    }
    

    【讨论】:

    • 谢谢,但是我应该在哪里写这两个代码,我认为我应该在 cellForItemAt 中写第一个,但是 reset()?在 viewdidload 中?
    • cell.reset 正上方switch 并实现cell类内部的功能
    【解决方案2】:

    我认为你应该研究 [collectionViewLayout] 委托方法。

    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)
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2016-09-06
      • 2018-11-19
      相关资源
      最近更新 更多