Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set effect on selected cell in uicollectionview(single selection) ios swift

I am using collection view for horizontal scrolling. Its working pretty well. Now I want to set any effect for selecting any cell. So, I wrote this code"

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {   
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell?.layer.borderColor = .None
        cell!.layer.borderWidth = 2.0
        cell!.layer.borderColor = UIColor.whiteColor().CGColor


        let dic1 = arr.objectAtIndex(indexPath.row) as! NSDictionary
        let url = dic1["url"] as! String
        let requestURL = NSURL(string:url)
        let request = NSURLRequest(URL: requestURL!)

        webviewfortab.loadRequest(request)


    }

This sets border for selected cell. But, when I select another cell, both the cell are getting selected but I want only single selection so that when user selects one,old one must be disselected.

like image 639
Govind Rakholiya Avatar asked Jan 12 '16 09:01

Govind Rakholiya


1 Answers

You just have to handle didDeselect

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)

And of course have set single selection on your collection view

collectionView.allowsMultipleSelection = false
like image 140
kostek Avatar answered Oct 04 '22 12:10

kostek