Here is my image of collectionViewCell .
I have collectionView inside tableViewCell and I have a button inside collectionViewCell which is working perfectly as my wish. But What is troubling me is the function didSelectItemAt is not being called .
I tried with
mycollectionview.isUserInteractionEnabled = true
mycollectionview.allowsSelection = true
but still not working . Is the button inside collectionViewCell is the cause behind it as I read somewhere . If its is so how can I fix the issue .
Please confirm that you have set the delegate for the UICollectionView to your ViewController not the TableViewCell. If you have set the Delegate to UITableViewCell, then your didSelectItemAt will be called inside the TableViewCell's class
try this code in swift 4
cell.btnCart.addTarget(self, action: #selector(btnCartAction), for: .touchUpInside)
and handle the action
#objc func btnCartAction(sender:UIButton) {
//write your code here
}
Check your all views which are placed on cell.If they have user interaction enabled and if user interaction enabled then what is the size of that item. It looks like some view is covering the whole cell that's why did select item does not get called.
Please check Your Button size ,tableView selection (single selection or Multiple), check tableView user Interaction is enable, also check datasource or delegate is connected
Related
Like written in the title, the didSelectedItemAt is bugging, but that's a little more complicated than in the title, I explain:
I try to code an application which is listing some stuffs. So I've got a ViewController with inside a TableView and a CollectionView. At the beginning the tableview's hidden is on false and the collectionview's one is on true. I've got a button on the navigation bar to switch CollectionView / TableView hidden true/false (I hope I'll be understandable with my bad English !). So the didSelectedRowAt is working well, but the didSelectedItemAt isn't. When I first click on a cell, nothing happen (on the screen at least), but the second click is working..... With the indexpath of the first click... I can't figure out why that's happening.
Does anyone know why?
Here is the some code of the app:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print (dogs[indexPath.row].name)
let vc = storyboard?.instantiateViewController(withIdentifier: "DogDetailsView") as? DogDetailsView
vc?.nameSend = dogs[indexPath.row].name
vc?.infoSend = dogs[indexPath.row].info
self.navigationController?.pushViewController(vc!, animated: true)
}
Thank you for helping
You are using wrong delegate method. Use following
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
The reason behind your didDeselect is working after the second tap is, you have turned on allowMultilpleSelection and whenever you tap first that cell its get selected, but you didn't implemented the correct delegate to hear that. when you tap once more, that cell is now deselected. That's why you are getting the callback in implemented method in such way.
I'm facing a weird issue in UICollectionView using iOS11 emulator. In my project i have a UICollectionView with UIImageView as cells and I've created segue as Triggered Segues for cells by dragging it to a view controller. It was working great but know the segue is not performing so i decided to remove the segue from cells Triggered Segues and I created a segue from my view controller to the destination view controller and performed segue from code
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
if let url = URL(string: "\(StringResources.serverAddress)/Content/images/Files/Thumb/\(photos[indexPath.row])") {
cell.image.downloadFrom(url: url)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("clicked")
performSegue(withIdentifier: "PhotoSegue", sender: collectionView.cellForItem(at: indexPath))
}
but it's not working either and there is no clicked printed in console. I've checked cell and UIImageView user interaction also UICollectionView delegate is ok. How can i fix it?
edit: I've found the problem. It's only calling when I'm double clicking on cell
There are some things that you could check and once all of them are in order it should work:
Check that your UICollectionView has both delegate and dataSource set.
Check that your UIImageView userInteractionEnabled property is set to false.
According to documentation:
Image views ignore user events by default. Normally, you use image views only to present visual content in your interface. If you want an image view to handle user interactions as well, change the value of its isUserInteractionEnabled property to true. After doing that, you can attach gesture recognizers or use any other event handling techniques to respond to touch events or other user-initiated events.
Check that both UICollectionView and parent have. userInteractionEnabled property is set to true.
Check that there are no other UIGestureRecognizers catching your touch event.
Check that there are no network requests freezing your UI.
Considering the information that you provided, I would try to remove the UIImageView from the cell and make sure that the cell touch is working before adding more elements.
the problem is that I was adding a UITapGestureRecognizer to dismiss the keyboard and that was catching touch events.
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}
The issue could be due to the fact that collection views have a didSelectItemAt method as well as a didDeselectItemAt method which alternate per click. This could explain why it only works on a double click (one click calls didSelect, the next calls didDeselect).
A quick (but not elegant) solution could be to implement your didDeselectItemAt and just use it to call the didSelectItemAt
I've been looking around for a while now but can't seem to work out how to stop the collection view cells from consuming the touch events.
I need the touch events to be passed down into the respective cells so that the buttons within the cells can be pressed. I was thinking i might need to work out how to disable the UICollectionView's didSelectCellAtIndexFunction?
I've also seen this as a potential solution: collectionView.cancelsTouchesInView = false
Also this link might help someone answer my question: How to add tap gesture to UICollectionView , while maintaining cell selection?
Thanks in advance!
Edit:
Also I should add: my buttons are added to a view that is in turn added to the cell's contentView. My code is done all programatically and so I am not using interface Builder at all.
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return false // all cell items you do not want to be selectable
}
Assuming all buttons are connected to the same selector, you need a way to differentiate which cell's button has been clicked. One of the ways for finding out the button's cell's index is:
func buttonPressed(button: UIButton) {
let touchPoint = collectionView.convertPoint(.zero, fromView: button)
if let indexPath = collectionView.indexPathForItemAtPoint(touchPoint) {
// now you know indexPath. You can get data or cell from here.
}
}
Try on your cell.
self.contentView.isUserInteractionEnabled = false
I am making an iOS app that relies on a table view. In each cell of the table view, there are 4 buttons aligned on the bottom. I have a cell class that is pretty standard and a feedController to handle the table and setting all the items of the cell.
Everything works fine on it but I can not figure out how to handle the button clicks within the cell. I can hard code it into my cell class, but then every 3 cells has the same interaction. Is there a way to pass the button click function from the cell class into the controller? I have tried checking the state from the controller and that has not worked.
Can you add a gesture recognizer as you're doing your cellForItemAtIndexPath? So I had something similar with a collection view, and what I did was as it within:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MyCollectionView
...
I would add a gesture recognizer to each cell
i.e.
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action:Selector("tapAction:")))
And then something like:
func tapAction(recognizer: UITapGestureRecognizer) {
...
}
so recognizer ends up being the specific item tapped, and I could take action accordingly (in my case, I had my datasource of items and I would find the item in an array by casting recognizer to a cell, finding the appropriate subview, and update values on it)
I would add code block properties to your cell class which the table can assign to deal with each button. In your cell, code each button handler to call the appropriate block, or pass an index for the button used in a single block.
See my answer here which has an example, but for a switch.
How can I get index path of cell on switch change event in section based table view
If after a few cells you get the same interaction, it's possibly because you're dequeueing a reusable cell, and you're getting the same cell.
Make sure to set your .setTarget() call for your buttons in your tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) data source every time the cell is dequeued. It would help if you shared how you're handling dequeuing to see if this is your issue.
I'm having a UICollectionView that holds pictures as elements in its datastore.
I want to load a high resolution pic into the element only when its corresponding UICollectionViewCell is currently showing on screen. Later, when the UICollectionViewCell goes off screen, I want to return the element's UIImage into the low resolution version.
My question is, how can I detect when a UICollectionViewCell is going off screen?
(I tried using the prepareForReuse method but I can't predict when it will be called).
I'm currently using a piece of code that sits in scrollViewDidScroll, and every time that the view scrolls I'm checking the self.collectionView.visibleCells to see which cells has scrolled off screen.
It seems a bit of an overhead and I wonder if there is a method called on the UICollectionViewCell itself whenever it is being scrolled of screen ?
The collectionView:didEndDisplayingCell:forItemAtIndexPath: method on UICollectionViewDelegate should do what you want.
From Documentation. collectionView:didEndDisplayingCell is called right after it finishes displaying, not when it goes off screen
Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears
collectionView:didEndDisplayingCell:forItemAtIndexPath: is the correct method to detect when a cell has gone from screen.
Meanwhile, I think it's more correct not to perform cleanup in collectionView:didEndDisplayingCell:forItemAtIndexPath: but to tell your cell to perform cleanup itself:
func collectionView(_ collectionView: UICollectionView,
didEndDisplaying cell: UICollectionViewCell,
forItemAt indexPath: IndexPath) {
cell.prepareForReuse()
}
With this approach your UICollectionViewDelegate doesn't have to know any implementation details of your UICollectionViewCell subclass. In the cell we'll override prepareForReuse method:
override func prepareForReuse() {
super.prepareForReuse()
imageView.image = lowResolutionImage
highResolutionImage = nil
}