如何在uicollectionView中更改角落单元格的背景颜色

时间:2018-09-25 00:01:08

标签: ios swift uicollectionviewcell

我必须在UICollevtionView中用红色显示角落单元格,并用绿色显示单元格的其余部分!enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在cellForItemAtindexPath中添加特定索引路径的背景色

func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "YouCustomCellIdentifier",
                                                   for: indexPath)
    guard let checkMarkCell =  cell as? YouCustomCell else {
        return cell
    }
    if(indexPath.row == 0) { 
        cell.backgrouColor = UIColor.red
   } else {
        cell.backgrouColor = UIColor.green
    }
  return checkMarkCell
}