【问题标题】:How to implement different collection view cell types in a single collection View?如何在单个集合视图中实现不同的集合视图单元格类型?
【发布时间】:2017-11-21 11:35:43
【问题描述】:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let dev = devices[indexPath.item]
    if dev.cuid == "3011"{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LightViewCell", for: indexPath) as! LightViewCell
        let visibility = self.getStatusFor(nodeID: dev.node_id!, endpoint: dev.end_point!, devicename: dev.device_name!)
        if visibility.contains("0"){
            cell.LigntIMG.image = #imageLiteral(resourceName: "bulb-off")
            cell.LightName.text = dev.device_name
            cell.status = 0
            cell.index = indexPath
            cell.controlCMDs = getControlCMDS(nodeID: dev.node_id!, endpoint: dev.end_point!)
            cells.append(cell)
        }else{
            cell.LigntIMG.image = UIImage(named: dev.menu_id!)
            cell.LightName.text = dev.device_name
            cell.status = 1
            cell.index = indexPath
            cell.controlCMDs = getControlCMDS(nodeID: dev.node_id!, endpoint: dev.end_point!)
            cells.append(cell)
        }
          return cell
    }


        else if dev.cuid == "3006"{
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DimmerLightViewCell", for: indexPath) as! DimmerLightViewCell
          return cell
        }

    return //
    }

根据我需要显示两个单元格中的任何一个的条件,我有两种类型的自定义单元格(LightViewCell/DimmerLightViewCell)...... 类型

这里我没有返回任何值,忽略该错误.....我需要知道如何实现上述要求..

谢谢:)

【问题讨论】:

  • 为什么不按照indexPath分类?
  • @Anurag 我听不懂你...你能解释清楚吗...
  • 让我试试...谢谢

标签: ios swift3 uicollectionview uicollectionviewcell


【解决方案1】:

该方法必须返回单元格。你可以试试这个代码:

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

    let dev = devices[indexPath.item]
    if dev.cuid == "3011"{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LightViewCell", for: indexPath) as! LightViewCell
        let visibility = self.getStatusFor(nodeID: dev.node_id!, endpoint: dev.end_point!, devicename: dev.device_name!)
        if visibility.contains("0"){
            cell.LigntIMG.image = #imageLiteral(resourceName: "bulb-off")
            cell.LightName.text = dev.device_name
            cell.status = 0
            cell.index = indexPath
            cell.controlCMDs = getControlCMDS(nodeID: dev.node_id!, endpoint: dev.end_point!)
            cells.append(cell)
        }else{
            cell.LigntIMG.image = UIImage(named: dev.menu_id!)
            cell.LightName.text = dev.device_name
            cell.status = 1
            cell.index = indexPath
            cell.controlCMDs = getControlCMDS(nodeID: dev.node_id!, endpoint: dev.end_point!)
            cells.append(cell)
        }
          return cell
    } else {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LightViewCell", for: indexPath) as! DimmerLightViewCell
          return cell
        }

    return //
    }

【讨论】:

    【解决方案2】:

    你可以通过在cellForRowAtIndexPath中使用你的条件来做到这一点

    举例

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if(indexPath.item > 0){
             let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LightViewCell", for: indexPath) as! LightViewCell
             /*do setup here*/
    
             return cell
       }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DimmerLightViewCell", for: indexPath) as! DimmerLightViewCell
            /*do setup here*/
    
            return cell
       }    
     }
    

    【讨论】:

    • 您可以根据您的要求更改条件我只是为collectionview部分的第一个单元格设置了条件...
    • 抱歉,我只在 iOS 中工作了过去两年...我无法获得您的程序...您能否向我推荐有关该索引路径的任何教程...以及上述的工作原理
    【解决方案3】:

    我不完全明白您面临的问题是什么。

    但是,您可以根据代码中的给定条件尝试以下代码返回单元格:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let dev = devices[indexPath.item]
        if dev.cuid == "3011"
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LightViewCell", for: indexPath) as! LightViewCell
    
            let visibility = self.getStatusFor(nodeID: dev.node_id!, endpoint: dev.end_point!, devicename: dev.device_name!)
            cell.status = visibility.contains("0") ? 0 : 1
            cell.LigntIMG.image = visibility.contains("0") ? #imageLiteral(resourceName: "bulb-off") : UIImage(named: dev.menu_id!)
            cell.LightName.text = dev.device_name
            cell.index = indexPath
            cell.controlCMDs = getControlCMDS(nodeID: dev.node_id!, endpoint: dev.end_point!)
            cells.append(cell)
            return cell
        }
        else if dev.cuid == "3006"
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DimmerLightViewCell", for: indexPath) as! DimmerLightViewCell
            return cell
        }
        return UICollectionViewCell()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2016-02-26
      相关资源
      最近更新 更多