Objective c Obj-c/Cocoa touch:UITableViewCell-CellTextField是否仅接受数字字符?
我希望UITableViewCell中的CellTextField只接受数字字符。 我以编程方式创建UITableViewCells,如下所示:Objective c Obj-c/Cocoa touch:UITableViewCell-CellTextField是否仅接受数字字符?,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我希望UITableViewCell中的CellTextField只接受数字字符。 我以编程方式创建UITableViewCells,如下所示: -(UITableViewCell*) getCellContentView: (NSString*) cellIdentifier { ... UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIden
-(UITableViewCell*) getCellContentView: (NSString*) cellIdentifier {
...
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:cellIdentifier];
...
CellTextField *txt;
txt = [[CellTextField alloc] initWithFrame:amountFrame];
... //here we should setup a delegate or similar to handle char validation for txt
[cell.contentView addSubview:txt];
return cell;
}
对于UITextField组件,我们有一个委托'shouldChangeCharactersInRange',但我找不到CellTextFields的等效委托。试试这个
h
如何将其与特定(以编程方式创建的)CellTextField进行实际连接?我假设CellTextField是UITextField的一种类型,不是吗?CellTextField必须是UITextField的一个子类。在这种情况下,您可以将类注册为CellTextField实例的委托。您能告诉我其他地方引用shouldChangeCharactersRange的位置吗?我不能完全理解这一点。我在视图中有许多不同的文本字段,但我只想检查一些特定的文本字段。你在使用这个类吗?如果是,请注意它实现了UITextFieldDelegate。如果需要,您可以将上面的代码分别添加到其.m和.h中
@interface YourClass : ParentClass <UITextFieldDelegate>{
NSNumberFormatter *integerNumberFormatter;
}
@end
-(void)viewDidLoad
{
[integerNumberFormatter setMaximumFractionDigits:0];
yourTextField.delegate = self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* proposedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSNumber *proposedNumber = [integerNumberFormatter numberFromString:proposedString];
if (proposedNumber) {
NSString *integerString = [integerNumberFormatter stringFromNumber:proposedNumber];
if ([integerString isEqualToString:proposedString]) {
// proposed string is an integer
return YES;
}
}
return NO;
}