-1

我有一个textfield和一个tableview。用户可以textfieldtableview或任何其他数据中选择数据。但是在输入用户自己的数据后,我tableview必须消失。我怎么能做到这一点。如果我使用辞职,第一响应者did select row方法不起作用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;
}

如果我这样使用,如果我选择行,它不会在索引路径方法中进入选择行。帮我

我没有得到我必须使用 resignfirstresponder 的地方

4

2 回答 2

1

resignFirstResponder用于解散第一响应者(在 的情况下为键盘UITextField)。如果您希望 tableview 消失,请将hidden属性设置为 true 或从视图层次结构中删除 tableview。

IE;

[tableView setHidden:YES]

或者

[tableView removeFromSuperview];

更新: 如果使用手势识别器检查父视图上的点击,您可以执行以下操作,以免不必要地触发手势方法。

我假设您正在视图控制器中为整个事情编写所有这些代码。

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

然后在视图控制器中实现以下方法(使其符合UIGestureRecognizerDelegate协议):

- (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.
  }
}
于 2013-03-14T14:36:20.613 回答
0

我认为这对您很有用。我使用了自定义单元格,并且在这里提供了我的工作代码。

    - (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];
}
于 2013-03-14T15:03:43.987 回答