When i use UITapGestureRecognizer for tab UITextfield only in section 0, but section 1 some row cannot tab when select tableviewcell

StackOverflow https://stackoverflow.com/questions/22219532

Question

My TableView Have 2 Sections , In Section 0 Have only 1 Row I make UITextField in this, I want to edit in UITextfield (section0,row0)

but don't want to tab for select row (section0,row0)

I use UITapGestureRecognizer this Problem can Fixed

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
    if(indexPath.section == 0) {
       UITapGestureRecognizer *tapRecognizer = 
       [[UITapGestureRecognizer alloc]initWithTarget:self 
                                              action:@selector(didTapAnywhere:)];
       [cell.contentView addGestureRecognizer:tapRecognizer];
}
...
}

But when I scroll the table and click in section 1 in Row 3,row6,row9,… it cannot tab to call didSelectRowAtIndexPath function.

When I comment out //UITapGestureRecognizer section 1 in Row 3,row6,row9,… can select but Section 0 row 0 Can select too (I don’t want).

I think this problem about draw cellForRowAtIndexpath when scroll, but I don’t know way to solve this problem.

thank you

Picture sample problem:

http://i.stack.imgur.com/lmosN.jpg

Was it helpful?

Solution

If you want just edit but don't want to tab for select row (section0,row0) you don't need add tapRecognizer to do it. You better do

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self.view endEditing:YES];
        return;
    }
    // an other section and row code
}

If you want to not change cell color when tap at cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // code init cell here
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else {
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
}

OTHER TIPS

Add the following code in first cell in cellForRowAtIndexPath

cell.userInteractionEnabled = NO;

I believe you use dequeueReusableCellWithReuseIdentifier in method tableView:cellForRowAtIndexPath.

If you do use it, then the reason behind is, when you scroll down to section 1, the tableview will automatically check the cell pool and get an available cell with the same identifier and assign to each row in section 1. If no cell is found then you will have some thing like:

if (cell == nil)
{
    //Create new cell with an identifier
}

Since your 1st row and section 0 (the text field) is not visible anymore and will be returned to the cell pool, the table view will take it and use for a random row in section 1 (for your case is 3, 6, 9, etc.). That's why you can't select those rows in section 1,

So tweak ur code and try if it helps:

@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
- (void) viewDidLoad{
    //Do stuffs as usual
    self.tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapAnywhere:)];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ..
    if(indexPath.section == 0)
    {
        //cell don't have the tap recognizer yet
        if (cell.contentView.gestureRecognizers == nil || ![cell.contentView.gestureRecognizers containsObject:self.tapRecognizer])
        {
            if (self.tapRecognizer.view != nil)
                [self.tabRecognizer.view removeGestureRecognizer:self.tabRecognizer];
            [cell.contentView addGestureRecognizer:tapRecognizer];
        }
    }
    else if (cell.contentView.gestureRecognizers != nil && [cell.contentView.gestureRecognizers containsObject:self.tapRecognizer])
        [cell.contentView removeGestureRecognizer:self.tabRecognizer];


   ..

}

That's the logic, you can based on that to solve your problem :-D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top