Question

I have a textfield and a tableview. User may select textfield data from tableview or any other data.but after entering user own data my tableview must disappear.how can i achieve this.if i use resign first responder did select rowmethod is not working in tableview.

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

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        txtcity.text = selectedCell.textLabel.text;


      }
     -(void)hideKeyboard  //this is my gesture recogniser method
      {

         autocompletetableview.hidden=true;//if i didn't use this statement it enter into didselectrow method
      }
     - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


   autocompleteTableView.hidden=YES;
    if( textField == txtcity)
   {


    autocompleteTableView.hidden = NO;

     NSString *substring = [NSString stringWithString:textField.text];
         substring = [substring stringByReplacingCharactersInRange:range withString:string];
     [self searchAutocompleteEntriesWithSubstring:substring];// this method loads data    into my tableview



      return YES;
     }


   }

   (BOOL)textFieldShouldEndEditing:(UITextField *)textField
   {
    if([txtcity.text isEqualToString:@""])
    {
    autocompleteTableView.hidden=YES;

    return YES;
     }
    else
    autocompleteTableView.hidden=NO;
   }

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {


   return arr2.count;
 }

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

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleDefault        reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}

     cell.textLabel.text = [[arr2 objectAtIndex:indexPath.row]objectForKey:@"Name"];
    cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
     cell.textLabel.numberOfLines=0;

   return cell;
}

if i use like this if i select row it doesn't enter into did select row at index path method. help me

i am not getting where i have to use resignfirstresponder

Was it helpful?

Solution

resignFirstResponder is used to dismiss the first responder ( keyboard in the case of UITextField). If you want the tableview to disappear set the hidden property to true or remove the tableview from the view hierarchy.

i.e;

[tableView setHidden:YES]

or

[tableView removeFromSuperview];

UPDATE: If using gesture recognizer for checking on the tap on parent view, you can do the following so that the gesture method is not fired unnecessarily.

I'm assuming you are writing all this code in the view controller for the whole thing.

UITapGestureRecognizer *tapGe = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
tapGe.numberOfTapsRequired = 1;
tapGe.delegate =self;
[self.view addGestureRecognizer:tapGe]

Then implement the following method in the view controller (Make it conform to UIGestureRecognizerDelegate protocol):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  If(touch.view==self.view){
     return YES; //If its the main view accept the touch
  }else{
     return NO; //Otherwise(say tableview) don't consume the touch.
  }
}

OTHER TIPS

I think It's useful to you.I have used custom cell and I have give my working code here.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [mainDetaileArray count];
}

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


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



    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"ProductMerchantCell" owner:nil options:nil];

        for (UIView *view in views)
        {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (ProductMerchantCell*)view;
                //cell.img=@"date.png";

            }
        }
    }

    cell.selectionStyle=UITableViewCellSelectionStyleNone;



        txtfield = [[UITextField alloc]initWithFrame:CGRectMake(104, 36, 58, 31)];
        txtfield.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
        txtfield.delegate = self;
        txtfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        txtfield.returnKeyType = UIReturnKeyDone;
        indexVal = indexPath.row;

        [txtfield setBorderStyle:UITextBorderStyleRoundedRect];
        txtfield.textAlignment = UITextAlignmentCenter;
        txtfield.text = [quantity_array objectAtIndex:indexPath.row];
   // NSLog(@" txtfield.text %@", txtfield.text);
        [txtfield setTag:indexPath.row];

        [txtfield setAutocapitalizationType:UITextAutocapitalizationTypeWords];


        NSString *total_str_price1=[[mainDetaileArray objectAtIndex:indexPath.row]valueForKey:@"total_product_price"];

        cell.availbl_pro_lbl.text=[NSString stringWithFormat:@"Total : %@%@",add_currnce_str,total_str_price1];

        cell.title_pro_lbl.text=[[mainDetaileArray objectAtIndex:indexPath.row]valueForKey:@"product_title"];

        str_price=[[mainDetaileArray objectAtIndex:indexPath.row]valueForKey:@"product_price"];

        cell.price_Pro_lbl.text=[NSString stringWithFormat:@"Price : %@%@",add_currnce_str,str_price];

        [cell.contentView addSubview:txtfield];


    return cell;

   }
- (void)textFieldDidEndEditing:(UITextField *)textField
{

    addtoValue = textField.text;
    [quantity_array replaceObjectAtIndex:textField.tag withObject:textField.text];


   // NSLog(@"quantity_array %@",quantity_array);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];
    [cell.Quantity_txt resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [cell.Quantity_txt resignFirstResponder];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top