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)基于我需要显示两个单元格中的任何一个的条件...... 类型
这里我没有返回任何值,忽略该错误.....我需要知道如何实现上述要求..
谢谢:)
答案 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!)
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 //
}
答案 1 :(得分:0)
您只需使用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
}
}
答案 2 :(得分: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()
}