如何在文本字段上返回键盘,同时在ios中的tableview单元上的文本字段

问题描述:

我如何返回textfield上的键盘,而我已经把textfield放在cell.contentview上,我正在使用textfieldshouldreturn method但它不工作所以请告诉我如何解决它呢? 这是对的tableview细胞我的一套文本框代码如何在文本字段上返回键盘,同时在ios中的tableview单元上的文本字段

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellidentitifier = @"cell"; 
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentitifier]; 
    if (cell ==nil) 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:cellidentitifier]; 

    } 
    else 
    { 
     [cell prepareForReuse]; 
    } 

    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 70, 70)]; 
    imgview.backgroundColor = [UIColor blackColor]; 
    [cell.contentView addSubview:imgview]; 

    UITextField *_nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 10, 160, 22)]; 
    _nameTF.placeholder [email protected]"Client Name"; 
    _nameTF.layer.borderWidth = 1.0; 
    [cell.contentView addSubview:_nameTF]; 
    _nameTF = nil; 

    UITextField *_timeTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 37, 160, 22)]; 
    _timeTF.placeholder = @"Time"; 
    _timeTF.layer.borderWidth= 1.0; 
    [cell.contentView addSubview:_timeTF]; 
    _timeTF = nil; 


    UITextField *_PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 64, 160, 22)]; 
    _PhoneTF.placeholder = @"Phone"; 
    _PhoneTF.layer.borderWidth= 1.0; 
    [cell.contentView addSubview:_PhoneTF]; 
    _PhoneTF = nil; 



    return cell; 
} 
+0

的2种方式来摆脱键盘的是告诉视图编辑结束像这样:[self.view endEditing:YES];或者告诉文本视图退出第一响应者[textView resignFirsResponder](如果你想以这种方式解除,可以在textfieldshouldreturn中调用) – DBoyer 2014-10-09 04:55:43

+0

为所有文本字段设置委托自身,并在textfieldshouldreturn委托操作中设置resignfirstresponder。 – Max 2014-10-09 05:33:26

组代表到的cellForRowAtIndexPath文本框下面,以前return cell声明。

_nameTF.delegate = self; 
_timeTF.delegate = self; 
_PhoneTF.delegate = self; 

-(BOOL)textFieldShouldReturn:(UITextField *)textField{ 

    [textField resignFirstResponder]; 

    return YES; 
} 

和下面的viewcontroller组代表在.h文件中

@interface myviewVC : UIViewController<UITextFieldDelegate>