【问题标题】: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.. 已经得到标签的实例,它工作正常,,,但是单个字符被读取两次

标签: ios objective-c uitableview uitextfield


【解决方案1】:

当你用

重新加载单元格时

[self.tableViewSkuVoids reloadRowsAtIndexPaths:@[selectedTxtFieldPath] withRowAnimation:UITableViewRowAnimationFade];

它将为您的文本字段提供resignFirstResponder,并关闭键盘

唯一的解决方案是手动调整单元格高度和表格内容高度https://stackoverflow.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;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多