Collectionview Moving slider in row does not select the row

Hello,


I have a pretty simple setup... each row in my Collectionview has a slider and "percent" label below it. When the user adjusts the slider I want to update the percent label. The problem is that adjusting the slider doesn't select the row so I can't get the indexPath. Without the index path I can't get the item so I can't access the label.


I have seen two suggestions repeatedly... one tries to use "superview", but that won't work because the type casting doesn't line up. The other has multiple iterations of trying to use the coordinates of the tap, but I can't get any of them to work.


Thank you!

Accepted Reply

I'm confused about what you are asking then. Isn't that "slider position changed" method in your collection view cell class? If so, you have direct access to anything in the cell within that method. If you need the index path, so you can know which data it came from and do other things, do that part in the refreshField function using the collection view's indexPath(for:) method as I explained above.


You should not set the object in the addObserver statement, only in the post statement. If you set object to something in the addObserver statement, you will only get notifications from that one object.


As Cluade31 suggested, posting the code you have so far might help.

Replies

Modify the refreshFields function like this:

@objc func refreshFields(_ notification: Notification) {
    let myCell = notification.object as! MyCVCellClass
    // you now have access to any properties/outlets you have defined in your MyCVCellClass
    // for example myCell.theSlider or whatever you called the outlet for the slider
    // you can also do something like:
    let cellPath = myCollectionView.indexPath(for: myCell)
}

Modify the addObserver statement like this (not 100% sure if I have the Swift syntax right, but it should be close):

NotificationCenter.default.addObserver(self, selector: #selector(refreshFields(_:)), name: .kRefresh, object: nil)

Sorry, I don't think I worded that clearly... In the "slider position changed" method, I don't know how to get that reference.


Also, when I do get the reference does it go here: "object: nil", replacing the "nil"

Could you post the slider position changed method you have written, that will be easier to help.


yes, the reference will replace the nil.

I'm confused about what you are asking then. Isn't that "slider position changed" method in your collection view cell class? If so, you have direct access to anything in the cell within that method. If you need the index path, so you can know which data it came from and do other things, do that part in the refreshField function using the collection view's indexPath(for:) method as I explained above.


You should not set the object in the addObserver statement, only in the post statement. If you set object to something in the addObserver statement, you will only get notifications from that one object.


As Cluade31 suggested, posting the code you have so far might help.

I know this is already a long thread and I haven't read all of it so appollogies if this is already said or not relevant (I'm pretty new to Swift etc.).


I have a collection view of images, to get the image tapped I added a gesture recogniser to the image view. Note I'm only doing this to add a border to selected items. In your case you may have to specify a custom cell class to get to the slider.


Something like:

// In viewDidLoad

let imagesCVGR = UITapGestureRecognizer(target: self, action: #selector(myTappedFunc))

imagesCVGR.delegate = self

imagesCVGR.cancelsTouchesInView = false

imagesCollectionView.addGestureRecognizer(imagesCVGR)



@objc func myTappedFunc(sender: UITapGestureRecognizer){

var myRow = 0

var indexPath: IndexPath = IndexPath()

var loc: CGPoint

// Get the point tapped and then the index path for the cell at that point.

loc = sender.location(in: imagesCollectionView)

if let iP = imagesCollectionView.indexPathForItem(at: loc) {

indexPath = iP

myRow = iP.row

}


// Get the cell that was tapped from the indexPath. You should now be able

// to get to the slider etc from within the cell.

let cell = imagesCollectionView.cellForItem(at: indexPath as IndexPath)


// Etc.


}

That's where the confusion is... the slider position changed method is in the ViewController. Now that I think about it, it makes more sense to pass have ieach collectionview item have a CLightBulb object since each one represents a light bulb and all the functionality should be contained in the item class. Thanks for all your time and help... you to Cluade31!