【发布时间】:2015-11-10 11:25:41
【问题描述】:
嗨,我在 customtableviewcell 中有一个 UILABEL 和 UITEXTFIELD。我需要在每个字符更改时更新 UILABEL。当我更新文本字段键盘中的每个字符时,我什至尝试使用通知中心。任何快速帮助将不胜感激
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
NSString *rateValue=[textField.text stringByReplacingCharactersInRange:range withString:string];
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
NSString *resultString = [[rateValue componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
[arrayRates replaceObjectAtIndex:textField.tag withObject:resultString];
selectedTextField=textField;
selectedTxtFieldPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
[self.tableViewSkuVoids beginUpdates];
[self.tableViewSkuVoids reloadRowsAtIndexPaths:@[selectedTxtFieldPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableViewSkuVoids endUpdates];
[[self.tableViewSkuVoids superview] endEditing:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:@"keyboardWillShow" object:nil userInfo:nil];
[selectedTextField becomeFirstResponder];
return YES;
}
【问题讨论】:
-
[selectedTextField becomeFirstResponder];每次调用此方法时都会调用键盘您可以从其他地方调用这行代码吗,例如 -(void)textFieldDidBeginEditing:(UITextField *)textField{}
-
为什么要重新加载单元格?开始编辑时获取对标签的引用,然后直接更新。
-
Avi.. 已经得到标签的实例,它工作正常,,,但是单个字符被读取两次
标签: ios objective-c uitableview uitextfield