37

I am creating one table view based application. I have created a custom table cell for table, that contains 2 labels, 1 image and 1 button. The table view Data source method is working properly. I am using xib for both custom cell and view controller class and i connect delegate and data source to the file's owner. But the problem is when i select the table row, didSelectRowAtIndexPath is not getting fire. As mentioned the only way to fire it is to hold down on the cell for about 3-4 seconds. Does anyone have any idea why this is happening?

Thanks for any pointers...

Here is my table view methods..

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [finalAddonsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NewCustomCell *cell = (NewCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"NewCustomCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }

    Addons *addons1=[[Addons alloc]init];
    addons1= [finalAddonsArray objectAtIndex:indexPath.row];

    if (addons1.data == nil) {
        cell.ivCategory.image = [UIImage imageNamed:@"blogo.jpg"];
    }
    else
    {
        cell.ivCategory.image=[UIImage imageWithData:addons1.data];
    }

    cell.lblTitle.text = addons1.name;
    if (addons1.price == nil) {
        cell.lblPrice.text = nil;
    }
    else{
        cell.lblPrice.text = [NSString stringWithFormat:@"%@ rs",addons1.price];
    }
    [cell.button addTarget:self
                    action:@selector(editButtonPressed:)
          forControlEvents:UIControlEventTouchUpInside];

    cell.button.tag=indexPath.row;
    index = indexPath;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"sjcjksbcjksbcfkebscf1234567890");
}

One more thing i am getting that if i am using default UITableViewCell instead of custom cell then also my problem is same, delegate method is not getting fire.

Custom cell properties:

enter image description here enter image description here

iPatel
  • 46,010
  • 16
  • 115
  • 137
Anand Gautam
  • 2,541
  • 3
  • 34
  • 70
  • Where is the `didSelectRowAtIndexPath` method? – geekchic Aug 10 '13 at 06:14
  • Why do you determine it gets fired late? Are you absolutely sure it takes 3-4 seconds or does the action you expect only occur then? Add an NSLog or breakpoint to the first line of didSelectRowAtIndexPath. Also - what is the size of the button in the cell? – Stavash Aug 10 '13 at 06:16
  • @nikhitaI am updating my question with didSelectRowAtIndexPath method. – Anand Gautam Aug 10 '13 at 06:18
  • if you are pushing a view Controller from **didSelectRowAtIndexPath** i think nextViewcontroller load some data from web-service. thays why that pushing a viewcontroller take 3_4 second and you thought didSelectRowAtIndexPath selecting cell take some time. – Nitin Gohel Aug 10 '13 at 06:19
  • @StavashYes, i put the breakpoint, it is coming in the breakpoint after long press on table row cell. – Anand Gautam Aug 10 '13 at 06:19
  • @NitinNo i am not pushing any view controller, this table view is added in one UIView, that UIView only i have to make hidden. – Anand Gautam Aug 10 '13 at 06:21
  • Is there any subview on the custom cell which is consuming touches? – Amar Aug 10 '13 at 06:21
  • @AmarNo, i have one UIButton that action i am giving in cellForRowAtIndexPath method & an action is working fine on cell. – Anand Gautam Aug 10 '13 at 06:23
  • 1
    How about cell highlighting? Does it occur immediately or only after a delay as well? Any active UIGestureRecognizer around? – Stavash Aug 10 '13 at 06:29
  • @StavashYes, it is highlighting in single click only, but delegate does not fire. – Anand Gautam Aug 10 '13 at 06:46
  • @StavashThanks bro, i am using gesture reconizer for view controller class, after removing gesture..it is getting fire – Anand Gautam Aug 10 '13 at 06:52
  • @Anand if it is fire late then delegate is calling for sure...i think problem is in image data...i think it is image which is taking time to load..try disable the else part of cell.ivCategory.image – raghu_dev Aug 10 '13 at 06:53
  • @RaghuThanks, delegate was not firing bcoz of tap gesture... – Anand Gautam Aug 10 '13 at 06:56
  • You must have used some view over that tableview or any another gesture recognizer in superview of tableview – Chintan Aug 10 '13 at 07:50

7 Answers7

100

same problem happened with me because I have added a tap gesture recogniser over it. If you have used any gesture recognizer try removing it and check if it causing the problem.

EDIT: Solution as commented by the Ali: If you have used tap gesture you can use [tap setCancelsTouchesInView:NO];

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
3

As others suggested, [tap setCancelsTouchesInView:NO]; does the trick. However, I want to make one thing clear:

If you think that you did not implement tapgesture and are curious about why you had to add your view into the protected views, check out your class because most probably you have inherited some class and that class includes tap gesture recognizer in it.

In my case, I did the following:

- (NSMutableArray *)tapProtectedViews
{
    NSMutableArray *views = [super tapProtectedViews];
    [views addObject:self.mTableView];
    return views;
}

Edit for Swift 4+

Assuming you have a UITapGestureRecognizer instance named tapGesture:

func disableTapGesture(){
    tapGesture.cancelsTouchesInView = false
}

Or you can:

if self.view.gestureRecognizers?.isEmpty == false{
    for recognizer in self.view.gestureRecognizers!{
        self.view.removeGestureRecognizer(recognizer)
    }
 }
Eray Alparslan
  • 806
  • 7
  • 12
  • 1
    "most probably you have inherited some class and that class includes tap gesture recognizer" - This was the problem for me! – Undrea Dec 10 '19 at 18:01
2

I was faced with a similar issue:

For me, the problem was because my UITableView was added to an UIScrollView and more specifically to its contentView. It appears that inside the contentView, I had to stay press 2-3 sec to fire the didSelectRowAtIndexPath method.

I moved my TableView to self.view instead of contentView and it solved the problem!

Tib
  • 1,250
  • 13
  • 20
2

Maybe you will call the method

[tableView deselectRowAtIndexPath:indexPath animated:NO];

before Push ViewController or Other Operation. Like

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1. manual call this method to deSelect Other Cell
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    // 2. than do other operation
    PushViewController Or Some Animation ....
}

that`s solve my problem .

Beyond Chao
  • 115
  • 1
  • 5
0

Dear i faced the same problem. When i tapped the cell but didselectrowatindexpath was not called than it was suddenly called when i released the button after pressing it for few seconds.

If you are facing the same issue there must be a 1. UITapGestureRecognizer that is creating problem for you or 2. a scroll view in which you placed you table view.

Thus you should remove the gesture or the super scroll view in which your table view is placed

Bobby K
  • 11
  • 3
0

If you have custom gesture object on your view, check override func gestureRecognizerShouldBegin(_ gesture: UIGestureRecognizer) -> Bool delegate. Compare custom gesture with sender gesture, If its not custom gesture object, pass it to the the super. So system gestures/taps won't get blocked.

name-it
  • 2,238
  • 3
  • 19
  • 28
-2

I'm not sure about this, but Delays Content Touches might have something to do with it.

n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
  • In the first screenshot you posted, in the `ScrollView` section, the property `Delays Content Touches` is enabled. Try disabling it, and testing it. – n00bProgrammer Aug 10 '13 at 06:32