繁体   English   中英

iOS 7-在自定义tableViewCell中更改textField的键盘类型

[英]iOS 7 - change Keyboard type of a textField in a custom tableViewCell

我有一个应用程序,用户在其中选择tableView中的项目,然后单击行内的textField并放置一个数字(表示数量)。 我想做的是,当用户单击textField时,将出现符号和数字键盘,甚至更好的是数字键盘。 这是我到目前为止的内容:

在我用于自定义tableViewCell的类SearchResultTableViewCell.m ,我具有以下功能:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField setKeyboardType:UIKeyboardTypeNumberPad];
    [textField reloadInputViews];
    return YES;
}

到目前为止,它不起作用。 我也试着做

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [quantity setKeyboardType:UIKeyboardTypeNumberPad];
    [quantity reloadInputViews];
    return YES;
}

quantity是.h文件中声明的IBOutlet UITextField quantity

有任何想法吗? 先感谢您。

您应该在custom tableview cell's .h文件中声明textfield IBOutlet ,并在其中设置键盘类型

1)自定义单元的.m文件中的awakeFromNib

-(void)awakeFromNib
{
    [self.textField setKeyboardType:UIKeyboardTypeNumberPad];
}

2)tableview的datasource方法的cellForRowAtIndexPath

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

    // Init you cell as you are doing right now
    CustomCell* cell;

     [cell.textField setKeyboardType:UIKeyboardTypeNumberPad]; 
}

您将在“ textFieldShouldReturn ”处完成所有这些工作,这是将文本输入某个文本字段(数量文本字段或其他文本字段)的结尾

为什么不在textFieldShouldBeginEditing:(UITextField *)textField ”中工作 ,或者为什么不只是在情节提要或xib文件中设置“数量”文本字段的键盘类型呢?

您使用错误的方法来设置键盘!

当按下键盘上的返回按钮时,将调用textFieldShouldReturn

在代码中初始化textField或在.xib中设置属性时,或者在用户单击textField时要设置键盘类型时,必须将键盘类型设置为以下类型的方法textFieldShouldBeginEditing:(UITextField *)textField

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM