2017-06-19 34 views
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 if dev.cuid == "3006"{ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DimmerLightViewCell", for: indexPath) as! DimmerLightViewCell 
      return cell 
     } 

    return // 
    } 

我有兩種類型的基於i需要顯示兩個單元中的任一個的條件定製單元(LightViewCell/DimmerLightViewCell)的...... 類型如何在單個集合中實現不同的集合視圖單元格類型View?

我在這裏沒有返回任何價值,忽略錯誤.....我需要知道如何實現上述要求..

謝謝:)

+0

你爲什麼不按'indexPath'分類? –

+0

@Anurag我不能讓你...你能解釋清楚嗎... –

+0

讓我試試看...謝謝 –

回答

0

該方法必須返回小區。你可以試試這個代碼:用你的病情在cellForRowAtIndexPath

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 // 
    } 
+0

我需要爲那個特殊的cuid單獨返回..... –

0

,你可以通過簡單地做它實例

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 
    }  
} 
+0

你可以改變條件你的要求,我只是爲第一個單元的一個collectionview的條件... –

+0

Sry我在iOS中工作的只有過去兩個...我不能得到你的程序...你可以參考我關於該indexpath的任何教程。 ..以及如何上述工作 –

+0

讓我試試看... –

0

我完全不明白是什麼問題,你正面臨着。

但是,你可以嘗試返回根據細胞在代碼中給定的條件下面的代碼:

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() 
} 
相關問題