0

I have collection view (If the process is easier on a table view I can change it to that).

I need to display the index path of the Cell just before displaying the collection view cell.enter image description here

I have been trying to figure out how to work on this, here are the things I worked out (Nothing has been a good solution).

Firstly, I tried using a Collection view cell with an added Label (For numbering) and UIView (For showing the content next to it). But the shadow code is not working for the UIView on the collection view cell.

        @objc extension CALayer {
    func applySketchShadow(
        color: UIColor = .black,
        alpha: Float = 0.5,
        x: CGFloat = 0,
        y: CGFloat = 2,
        blur: CGFloat = 4,
        spread: CGFloat = 0)
    {
        shadowColor = color.cgColor
        shadowOpacity = alpha
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur / 2.0
        if spread == 0 {
            shadowPath = nil
        } else {
            let dx = -spread
            let rect = bounds.insetBy(dx: dx, dy: dx)
            shadowPath = UIBezierPath(rect: rect).cgPath
        }
    }
}

@objc extension UIView{

func applyShadowToView(){

    self.layer.borderWidth = 1.0
    self.layer.borderColor = UIColor.clear.cgColor
    self.layer.masksToBounds = true
    self.layer.masksToBounds = false
    self.layer.applySketchShadow(color: UIColor.black, alpha: 0.09, x: 3, y: 2, blur: 50, spread: 4)
}}

For some weird reasons, my shadow view is not showing up. I tried various codes from online just to be sure, nothing works out.

Other things I thought off are having a header view to show the number of the Collection view, but header view needs to be as wide as Frame so this is not an option and

the other thing is having 2 collection view cells, even cells showing the Number and odd cells showing content.

But the problem with this is I want to add rearranging functionality later on and this method will not work when I want to do it.

Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37
SriTeja Chilakamarri
  • 2,463
  • 4
  • 16
  • 29
  • You can do this with the table view. add a cell to table view with a label and view with shadow and just pass your index path to cell class to show index next to it. – Jarvis The Avenger Jan 31 '19 at 04:55
  • Create two cell one is for Description and the second one is for your index and description. create two enums of cell type and just display it according to your cell type. – Jarvis The Avenger Jan 31 '19 at 04:57
  • 3
    `1.` you can do same functionality from both collectionView or TableView but in your case tableView is much easier. `2.` As you said, you need to display cell number in every cell and content, better if you do this in a single cell with 2 labels. `3.` BTW cell will show shadow if anyhow its not working then add a view in cell as contentView & give it shadow, make sure contentview have spacing from all 4 directions. – dahiya_boy Jan 31 '19 at 05:07
  • 2
    Have you set background color for collectionviewcell? – RajeshKumar R Jan 31 '19 at 05:15
  • To show indexpath in cell use `indexPath.row` or `indexPath.item` – dahiya_boy Jan 31 '19 at 05:16
  • Possible duplicate of [UITableViewCell: rounded corners and shadow](https://stackoverflow.com/questions/37645408/uitableviewcell-rounded-corners-and-shadow) – dahiya_boy Jan 31 '19 at 05:18
  • 1
    I am unable to apply shadow to the UIView inside the collection view cell. But when I apply shadow to the Collectionview cell itself, it seems to be working just fine. Could some tell is there anything I have to specifically use for showing up a shadow on UIView which is inside the Collectionview cell. And yeah I have set a background view for Colelctionview cell @RajeshKumarR – SriTeja Chilakamarri Jan 31 '19 at 05:28
  • @dahiya_boy I tried doing this but had no luck, do you have any working source code for this or any demo project ? Thanks. – SriTeja Chilakamarri Jan 31 '19 at 05:29
  • @SritejaC your code is working when you give the shadow to cell. Now whats left? – dahiya_boy Jan 31 '19 at 05:34
  • I don't want the shadow to whole cell. I want to have label and a UIView inside the cell. When I apply shadow code the UIView inside the cell, the shadow does not work. Shadow only works when I apply to the collection view cell directly. – SriTeja Chilakamarri Jan 31 '19 at 05:44
  • 1
    `self.layer.masksToBounds` or `self.clipsToBounds` should be `false`. Otherwise, the shadow will be clipped. – wzso Jan 31 '19 at 06:39

0 Answers0