从表格视图单元格更新文本并刷新后键盘关闭
Posted
技术标签:
【中文标题】从表格视图单元格更新文本并刷新后键盘关闭【英文标题】:keyboard dismissing after updating text from tableview cell and refreshing 【发布时间】: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.. 已经得到标签的实例,它工作正常,,,但是单个字符被读取两次 【参考方案1】:当你用
重新加载单元格时[self.tableViewSkuVoids reloadRowsAtIndexPaths:@[selectedTxtFieldPath] withRowAnimation:UITableViewRowAnimationFade];
它将为您的文本字段提供resignFirstResponder
,并关闭键盘
唯一的解决方案是手动调整单元格高度和表格内容高度https://***.com/a/33621733/1060785
【讨论】:
【参考方案2】:这对我来说很好
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
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;
selectedIndexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
customCell *cell=(customCell *)[self.tableView cellForRowAtIndexPath: selectedIndexPath];
NSString *multiplierValue=someValue;
if ([resultString isEqualToString:@"0.0"])
cell.txtFieldRates.text=@"";
else
cell.txtFieldRates.text=[NSString stringWithFormat:@"$ %@",resultString];
NSString *customerValue=[NSString stringWithFormat:@"%d",[multiplierValue intValue]*[resultString intValue]*365];
if (customerValue.intValue<=0)
cell.lblCustomer.text=@"";
else
cell.lblCustomer.text=[NSString stringWithFormat:@"$ %@", customerValue];
return NO;
【讨论】:
以上是关于从表格视图单元格更新文本并刷新后键盘关闭的主要内容,如果未能解决你的问题,请参考以下文章