Prevent UITableViewCell highlighting when image in content view is touched - iphone

I have added a UIImageView to the right side of the content view of my UITableViewCell.
I am trying to prevent the cell from being highlighted while the user is touching the UIImageView (because an attached gesture recognizer performs some other functionality).
I have thought about implementing tableView:shouldHighlightRowAtIndexPath: and returning NO, but I am unsure how to easily determine if the image is being touched when this method is called. Is there an easy way to do this?

Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
// Add tap gesture recogniser to cell's image view
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped)];
tap.cancelsTouchesInView = YES;
cell.theImageView.userInteractionEnabled = YES;
[cell.theImageView addGestureRecognizer:tap];
return cell;
}
The important parts are to make sure the image view has userInteractionEnabled = YES and the gesture recogniser has cancelsTouchesInView = YES.
This will cause the tapped method to fire if the image view is tapped, but the cell will not be highlighted.

why don't you add a UIButton with type custom.Will have the tap button action.Will not affect the cell highlight
Prevent highlight of tablecell
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Related

UICollectionView delegate's tap method not getting called

I have a collection view, the datasource delegate works well, but UICollectionViewDelegate:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didselect");
}
not get called, although i set the delegate (as i did with data source and it worked)
I have to mention that my cell is loaded from a nib and is connected to a subclass of UICollectionViewCell, anyway the cells do not respond to my touch. I enabled the user interaction in the UIImageView that is in my cell.
also :
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"this is caled");
return YES;
}
is not getting called!
as I mentioned I did set:
[self.collectionView setDelegate:self];
and of course
<UICollectionViewDelegate>
also I don't have any touchBegan override ..
UPDATE:
WEIRD! it only gets called if I long press! how can I fix this, I set delaysContentTouches to NO plus i don`t have any gesture recognizers implemented.
help please. thanks.
It looks like there is a UITapGestureRecognizer somewhere up in the view hierarchy. By default, UITapGestureRecognizers consume the touch that they recieve, meaning that it is not passed to the views below it in the hierarchy. You need to find the rogue tap gesture and add this line
tapGestureRecognizer.cancelsTouchesInView = NO;
This will make it pass touches to views below it in the hierarchy, and hopefully solve your problem.
Looks like you've added TapGestureRecognizer somewhere and it prevents selecton of cell. Check them, that should be the problem.
I was facing the same issue, that clicking on the custom UICollectionView Cell, it was not detecting the click.
In my case, the problem was that in the CustomCell's Xib, the userInteraction was enabled and that's why UICollectionview's didSelectItemAtIndexPath was not getting called, instead user tap information was being sent to that particular cell for which I had no handler.
I had a similar issue with PSUICollectionView (this works on iOS5 too) and I fixed it by putting a button on my CollectionViewCell and setting the target of that button
Also add tag's to know which button is pressed.
In my case I had TapRecognizer added in self.view due to which all taps in Screen is recieved at self.view not in collectionViewDidSelect.
So take care of this .
in ur .h file, import CellViewController and add delegate
#import "myColleCell.h"
UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
in ur .m file,add the following codes to ur ViewDidLoad,
UINib *cellNib = [UINib nibWithNibName:#"myColleCell" bundle:nil];
[myCollectionView registerNib:cellNib forCellWithReuseIdentifier:#"myColleCell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(220, 220)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[myCollectionView setCollectionViewLayout:flowLayout];
and setup cell with ur CellViewController
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier= #"myColleCell";
myColleCell *cell = (myColleCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[cell setupCell:[dataArray objectAtIndex:indexPath.row]];
//setup cell function methods placed in your CellViewController
return cell;
}
and finally make sure that your cellView, collectionView are set user interactive to YES
Ensure there aren't any objects setting the userInteractionEnabled property to NO on the UICollectionViewController.
Similar to what other people are saying, I had this same problem and it was fixed by removing a call to userInteractionEnabled where the parent view was adding it as a child view controller. I was able to test this by adding a UIButton to the cell and determining that even it couldn't receive the touch events.
Adding here as a reference for other people who are looking for the answer
Short Answer:
Delay the touches of default gesture recognizers associated with the tableview:
if let gestures = tableView.gestureRecognizers{
for gesture in gestures {
gesture.delaysTouchesBegan = true
}
}
Explanation
Every tableview has gesture recognizers associated with it. Which causes the delays of touches to custom UItableView cell. Set the delaysTouchesBegan to true so that the touch can be passed to subviews quickly.
In my case it was CollectionViewController inside UItableViewCell for which collectionView:didSelectItemAtIndexPath was being called with a delay.
Maybe you should use a tap gesture on the collection view.

dismiss iPhone keyPad in dynamic UITableView

I have UITableView with two sections. First section is a static row with stepper which creates cells of second section. Each cell of second section contains the UITextField with keypad. I can dismiss the keypad using UITapGestureRecognizer or additional DONE button in keypad but it is working for the last cell only.
I have tried the following methods:
UITapGestureRecognizer in the table view
in my ViewDidLoad I put:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
and then
-(void)hideKeyboard{
[self.tableView resignFirstResponder];
}
or
-(void)hideKeyboard:(UITapGestureRecognizer*)sender{
[self.cellText endEditing:YES];
}
Use tags to recognize which textfield I am editing.
In
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I added
[cellText addTarget:self action:#selector(myNumberValueBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
and in
-(void)myNumberValueBeginEditing:(UITextField *)sender
{
int row = [sender.superview.superview tag];
UITextField *cellTemp = (UITextField*)[(UITableViewCell *)sender.superview viewWithTag:200+row];
cellTemp.delegate = self;
[cellTemp becomeFirstResponder];
}
then I am trying to resignFirstResponder in DONE button
-(IBAction)doneButton:(UITextField *)sender {
NSLog(#"doneButton");
int row = [sender.superview.superview tag];
[(UITextField*)[(UITableViewCell *)sender.superview viewWithTag:200+row] resignFirstResponder];
}
I have no more ideas how to resign the keypad from all UITextFields. If anyone has some remedy, I will really appreciate.
You were close with your use of endEditing:. Instead of sending endEditing: to an instance of your text field, try sending it to your main view. e.x:
[self.view endEditing:YES];
endEditing: can be sent directly to a text field instance, or to a view, in the case of the latter any text field that is editing that is a subview of the view you specified will resign first responder.
From the docs:
Causes the view (or one of its embedded text fields) to resign the
first responder status

Labels with targets to another view

I would like to have a login similar to this screenshot:
The problem is that I don't know how to do the bottom, these two options. When I push, I'd like to put another view — the sign up view or reset password view —, but I don't know how to do these elements (are they labels?).
They could be UIButton with the type Custom and with a value for the text:
To make this, First you take a UILabel to show the "Tumblr" option. Then you create a UITableView. This TableView would contain 2 sections like "Email" and "Password". Make the edges of the UITableView as rounded using
TableView.layer.cornerRadius = 10;
Now to push to another view you can simply write any data in the UITableViewCells in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and using the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
you can pass that information to the next view for the reset password option you mentioned likewise. Create a UIButton and name it as "Login".
I this is what you wanted to achieve ?? Any doubts please tell. Thanks :)
EDIT:
For the options below what you can do is that make the UIButtons as custom and write their text as "Reset Password" etc. Then on their click you can open a presentModalViewController of each of the next view's or in the same view itself, in which you would be able to do your sign in and reset stuff. Hope it helps !!!
It is a label and you link it with the action method.
//using UITapGestureRecognizer to easy to set target to another view
UITapGestureRecognizer *tab_1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(action_1)];
tab_1.delegate = self;
[label1 addGestureRecognizer: tab_1];
UITapGestureRecognizer *tab_2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(action_2)];
tab_2.delegate = self;
[label2 addGestureRecognizer: tab_2];
-(void)action_1{
//your action or view;
}
-(void)action_2{
//your action or view;
}

Hide Keyboard when user taps outside the text field in UITableView and UIScrollView

My UIViewController hierarchy is as follows
UIView
UIScrollView
UITableView
UITableViewCell
UITextField
The UITableView is added to the view controller programmatically.
I want to hide keyboard when user taps outside UTTextField either on the view or on the UITableView
I am executing some methods when user taps on other UITableView rows
I tried
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UIScrollView doesn't send the touch events.
I tried adding Tap gesture
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[[self view] addGestureRecognizer:singleTap];
but with TapGesture, hides the following event
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
are there any other possible ways to hide the keyboard?
use the code : [self.view endEditing:YES];
use the UITextFieldDelegate
and the method
– textFieldShouldEndEditing:(UITextField*) txtField
{
[txtField resignKeyPads];
return YES:
}
this can also done by the scrolview delgate too
-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//resign all keypads of all textfields use array containing keypads
}
one thing more is change the class of UIView to UIControl and make a method IBAction and connect the UIControl touchupInside to that ibaction, it will resign keypads
If you want to still use tap gestures you need to add the gesture recogniser to the table background like so:
[tableView.backgroundView addGestureRecognizer:singleTap];
This will prevent the hiding of:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
If you want to put a gesture recognizer on the background view you need to make sure it has one.
Add
self.tableView.backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];
UITableView didSelectRowAtIndexPath will not call when UITableview is Edit Mode. So you suppose to create custom gesture event to handle the same.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//cell design code goes here.
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
doubleTapGestureRecognizer.numberOfTapsRequired = 1;
//tapGestureRecognizer.delegate = self;
[cell addGestureRecognizer:doubleTapGestureRecognizer];
return cell;
}
//Handle the click event
-(void) handleDoubleTap:(UITapGestureRecognizer*)sender{
[self.view endEditing:YES];
UITableViewCell * cell =(UITableViewCell*) sender.view;
//get the selected table indexpath.
NSIndexPath * indexPath= [tblCart indexPathForCell:cell]; //to handle the scroll
tblCart scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
NSLog(#"Comming");
}

Custom touch area on tableview cell with checkbox iphone

I've added a checkbox to a table view cell which is a custom button with two images. Clicking this works fine as I can check and uncheck.
The problem is when you click the rest of the row it still selects the checkbox/button. If I comment out didSelectRowAtIndexPath it doesn't happen so I'm wondering how to get around this happening?
A solution I'd be happy with is to allow only selection of the Accessory Disclosure Indicator but there doesn't seem to be a way of doing this.
Sorry if this is a really badly asked question, I'm very tired! Let me know if you need code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WedAppDelegate *delegate = (WedAppDelegate *)[[UIApplication sharedApplication] delegate];
PlanTaskShowViewController *planTaskShowViewController = [[PlanTaskShowViewController alloc] initWithNibName:#"PlanTaskShowViewController" bundle:nil];
[delegate.navController pushViewController:planTaskShowViewControlleranimated:YES];
[planTaskShowViewController release];
}
put an button on the front instead of an image and do the selecting thing only when button is clicked (change the image of the button).
OR
put an on click event listener / gesture recognizer on your image
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeImage:)];
[imageView addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];
//EDIT:
If you use highlightedImage for your UIImageView or UIButton it will be highlighted when selecting the UITableViewCell. It's a really nice feature, but sometimes it's only confusing ;)
Check this :
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeImage:)];
[imageView addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];