I have designed a UITableViewCell in a .xib. The cell contains an UIImageView that only partially covers the cell. The behavior I am seeking is, if the user taps on the UIImageView, the UIImageView's UITapGestureRecognizer's selector gets triggered, and the UITableViewCell does not trigger didSelectRowAtIndexPath. If the user taps on the UITableViewCell but not the UIImageView, then I want the opposite behavior. Right know what I'm getting is, when the user taps on the UIImageView, both the UITapGestureRecognizer's selector and didSelectRowAtIndexPath get triggered. And when the user taps on just the UITableViewCell, everything works fine. How can I get the desired behavior? I have tried messing around with cancelsTouchesInView among other things.
You don't need to use gesture recognizer to the cell, instead allow userInteractionEnabled set to true for the imageView and add target to the imageView. Refer to this code:
yourImageView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped")
yourImageView.addGestureRecognizer(tapGesture)
Related
I have added UIButton(for some functionality requirement in project.) with checkbox image. Checkbox image select and unselect working proper on UITableViewCell tap but if I tap cell on checkbox button area button image isn't changing. I mean didSelectRowAt method isn't calling.
I found solution. I have checked that UIButton's UserInteraction ignores UITableViewCell's tap.
So I have just added below line in my code. And cell tap working good.
cell.btnCheck.isUserInteractionEnabled = false
So I have a UITableView nested inside a UITableViewCell, like:
UITableView baseTable
UITableViewCell baseTableCell0
UITableView subTableView
UITableViewCell subTableViewCell
UIView subView
UITableViewCell baseTableCell1
UICollectionView subCollectionView
UICollectionViewCell subCollectionViewCell
subView has a UITapGestureRecognizer connected, acts like a button
subTableView and subCollectionView should receives touch events and perform didSelectRowAtIndexPath
On iOS 8 this works fine, but when i'm testing on ios 7 the cells in subTableView and subCollectionView does not receives touch event anymore, the touch event is sent to [baseTableView tableview... didSelectRowAtIndexPath]. However the UITapGestureRecognizer on subView can still receive touch event.
update: ios 6 has this problem as well, so it only works on ios 8 so far
Use this code on tab gesture object
tap.cancelsTouchesInView = NO;
Try changing the background color of your UITableView, UITableViewCell and UIView, this way you can see if your child view is not exceeding your parent view, if it is the case your child view is not going to receive touch events. Then either adjust Parent to show child fully or adjust child to remain in the parent boundary.
Hope it helps!
EDIT:
May be passing touch events to subviews can help.
I have a tableview which is populated from core data using an
if let fetchCell = fetchedResultsController...
I'm trying to pass data from all my tableview cells to the gesture recognizer's function so when a cell is swiped over, the recognizer will recognize it and send the variables over to the gesture recognizer's function so that I can work with them.
var Swipe = UISwipeGestureRecognizer(target: self, action: Selector("swipes:"))
rightSwipe.direction = .Right
cell.addGestureRecognizer(Swipe)
The above is simply adding the gesture recognizer to each individual cell.
func swipes(sender:UISwipeGestureRecognizer) {
println("You just swiped the following: \(correct_string)")
}
The above is the actual function of what to do when a cell is swiped. How can I get a variable which was assigned to an individual cell in the if let to print inside the swipes function
Thank you. Hope I didn't confuse anyone.
Rather than using a separate gesture recogniser for each cell (which will, unless you're careful, cause problems with cell reuse), I would proceed as follows:
Create a single gesture recogniser and add it to the tableView.
When the gesture is recognised, in your swipes function, use the sender's locationInView(tableView) to identify the point where the swipe occurred.
Then use the tableView indexPathForRowAtPoint to determine the indexPath for the cell where the touch occurred.
Finally, use the fetchedResultsController's objectAtIndexPath function to obtain the relevant NSManagedObject.
(Presumably correct_string is an attribute of the NSManagedObject.)
So i have a custom UITableViewCell which contains three UILabel. The UITableViewCell is such that the UILabel completely cover the cell.
Now i want to detect whenever user taps on the cell. Problem is as the UILabel cover the cell I cannot use UITableView delegate method for detecting touch on the cell.
I thoughout about using gesture recoginzers on the UILabel but then i don't get the index of the touched cell. I also thought about placing a transparent button on top of the cells but here there is also the same problem that i can't get the index of the touched cell.
Can anybody guide me with an approach on how can i accomplish detecting taps with tableView didSelectRowAtIndexPath when UILabel are covering the cell.
Try to set userInteractionEnabled to false in all labels in cell. This will pass touch to the cell and then you can use UITableView delegates. Be sure that cell stays userInteractionEnabled = YES
To get the touch event besides didSelect method try this. In your custom cell class add a block like,
typedef void(^TappedCell)(CustomCell *cell);
and add a property for it,
#property (nonatomic, strong) TappedCell tappedCell;
and on transparent button action
- (IBAction)buttonTapped {
if (self.tappedCell) {
self.tappedCell(self);
}
}
And in cellForRow method
__weak type(self)weakSelf = self;
cell.tappedCell = ^ (CustomCell *tappedCell) {
//Ur actions goes here
//for getting index
NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:tappedCell];
[weakSelf someActionWithIndexPath:indexPath];
};
It doesnt matter whether UILabel hide your cell or UIImageView hide it. It will automatically detects tableView didSelectRowAtIndexPath. Check it first you wont face any problems. Did you try to implement tableview in detail first
I am trying to detect a touch event in UIImageView which is added in UITableViewCell's contentView. But, Imageview is not triggering the touch event, i have enabled the imageview userinteraction too. Basically, I am trying to drag image from UItableViewcell to somother view/place. do you guyz have the solution of it, dragging images from UITableViewcell to otherviews.
create a custom UIView and add ImageView on that view as a subview then add touch event on that view.
then add that view on Cell content View