10

So this is driving me nuts, whenever I tap an item in my UITableView it does nothing, but when i press and hold the UITableViewCell after about 3-5 seconds it decides to move forward and do what I want.. any thoughts why this might be happening?

Here's my code

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    _cell = [_arrayItems objectAtIndex:indexPath.row];
    _cell = nil;
     static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    _cell = (CustomWidget *)[tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (_cell == nil) {
        _cell = [[CustomWidget alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier title:[_arrayItems objectAtIndex:indexPath.row] subTitle:@"Custom subtitle"];
    }

    _cell.textLabel.text = [_arrayItems objectAtIndex:indexPath.row];
    _cell.textLabel.hidden = YES;
    return _cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrayItems.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    _passedInPageTitle = selectedCell.textLabel.text;
    [self openDetailPage];
}
  • Maybe your CustomWidget has a UIButton or something similar which became firstResponder and receiving all those events? Also, after didSelectRowAtIndexPath, I usually call ` [tableView deselectRowAtIndexPath:indexPath animated:YES];` to make sure it is getting deselected. – chuthan20 Dec 11 '13 at 03:46
  • 1
    If you put a breakpoint in didSelectRowAtIndexPath, does it get hit the second you tap an item or after 3-5 seconds? Unrelated, what are you accomplishing with the first two lines of cellForRowAtIndexPath? – Erik Kerber Dec 11 '13 at 03:46
  • Also, directly calling cellForRowAtIndexPath isn't very kosher. You should retrieve information you need from your model (_arrayItems) using the indexPath. For all you know, calling that could be creating an entirely new cell from memory. – Erik Kerber Dec 11 '13 at 03:49
  • Did you remember to set your tableview's delegate to the class that's implementing tableView:didSelectRowAtIndexPath:? And does this class declare that it implements UITableViewDelegate? – Alex Shepard Dec 11 '13 at 04:03
  • You inspired me to create a StackOverflow question I can reference from now on ;): http://stackoverflow.com/questions/20510288/is-calling-cellforrowatindexpath-ever-practical/20510328?noredirect=1#20510328 – Erik Kerber Dec 11 '13 at 04:15
  • Make sure that the table doesn't have delaysContentTouches set to YES. – rdelmar Dec 11 '13 at 04:55
  • 1. chuthan20 I did have a button there and removed it and I'm still getting the same result. 2. chuthan20 Thanks for the heads up I've added [tableView deselectRowAtIndexPath:indexPath animated:YES]; 3. ErikKerber I put a break point there and found that it does not get called the moment I touch the tableviewcell item. 4. ErikKerber With the first two lines I'm passing in an array of items i've created to test with. 5. ErikKerber Sorry, still somewhat of a noob, could you elaborate? –  Dec 11 '13 at 06:02
  • 6. AlexShepard Yeah, I checked it and in the header file I'm telling the UIView (file containing the tableview and a related UIView for searching) to use and then where the tableView is created I'm assigning the datasource and delegate. 7. rdelmar I've set the delaysContentTouches set to NO and still the same result. :( Thank you for the time spent on this, I'll keep trying to solve this one off issue :) –  Dec 11 '13 at 06:03
  • Try changing the code you have in didSelectRowAtIndexPath. You shouldn't get the string from the cell, you should get it with _arrayItems[indexPath.row]. So delete that first line,and change the right side of the second line to what I wrote above. I don't know if this will fix your problem, but you should do it this way anyway -- cells are for displaying data, not for providing it. – rdelmar Dec 11 '13 at 06:43

1 Answers1

9

I found my answer here - I had a UITapGestureRecognizer on the UITableView's parent view, which was capturing the tap.

Community
  • 1
  • 1
apb
  • 3,270
  • 3
  • 29
  • 23