简体   繁体   English

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

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

I have an application where the user selects items in a tableView and he clicks in a textField inside the row and places a number (for quantity). 我有一个应用程序,用户在其中选择tableView中的项目,然后单击行内的textField并放置一个数字(表示数量)。 What I want to do is that when the user clicks the textField, the symbols and numbers keyboard would appear, or even better, a number keypad. 我想做的是,当用户单击textField时,将出现符号和数字键盘,甚至更好的是数字键盘。 This is what I have so far: 这是我到目前为止的内容:

In my SearchResultTableViewCell.m class, the class I use for my custom tableViewCell, I have this function: 在我用于自定义tableViewCell的类SearchResultTableViewCell.m ,我具有以下功能:

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

So far, it does not work. 到目前为止,它不起作用。 I also tried to do 我也试着做

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

Where quantity is the IBOutlet UITextField as declared in the .h file. quantity是.h文件中声明的IBOutlet UITextField quantity

Any ideas anyone? 有任何想法吗? Thank you in advance. 先感谢您。

You should declare IBOutlet of textfield in custom tableview cell's .h file & set keyboard type in 您应该在custom tableview cell's .h文件中声明textfield IBOutlet ,并在其中设置键盘类型

1) awakeFromNib in custom cell's .m file, 1)自定义单元的.m文件中的awakeFromNib

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

2) cellForRowAtIndexPath of tableview's datasource method. 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]; 
}

You're doing all this work at " textFieldShouldReturn ", which is the end of entering in text into some text field (the quantity text field or some other text field). 您将在“ textFieldShouldReturn ”处完成所有这些工作,这是将文本输入某个文本字段(数量文本字段或其他文本字段)的结尾

Why not do this work in " textFieldShouldBeginEditing:(UITextField *)textField " , or why not just simply set the keyboard type of the Quantity text field in the storyboard or xib file? 为什么不在textFieldShouldBeginEditing:(UITextField *)textField ”中工作 ,或者为什么不只是在情节提要或xib文件中设置“数量”文本字段的键盘类型呢?

You are using the wrong method to set the keyboard! 您使用错误的方法来设置键盘!

textFieldShouldReturn is called when the return button on the keyboard is pressed. 当按下键盘上的返回按钮时,将调用textFieldShouldReturn

You must set the keyboard type when you are initializing the textField in the code or set the property in .xib or if you want to set the keyboard type when user clicks the textField then put your code in the following method textFieldShouldBeginEditing:(UITextField *)textField 在代码中初始化textField或在.xib中设置属性时,或者在用户单击textField时要设置键盘类型时,必须将键盘类型设置为以下类型的方法textFieldShouldBeginEditing:(UITextField *)textField

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

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