2

Hi i am having a UILABEL and UITEXTFIELD in a customtableviewcell.I need to update the UILABEL upon each character change.As i update the each character in the textfield keyboard is dismissing ..I even tried using Notification centre. Any quick help would be thankful

- (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;
}
Monicka
  • 505
  • 6
  • 20
  • [selectedTextField becomeFirstResponder]; calls the keyboard with every call of this method can you call this line of code from somewhere else, here for example -(void)textFieldDidBeginEditing:(UITextField *)textField{} – Monicka Nov 10 '15 at 11:33
  • Why are you reloading the cell? Get a reference to the label when editing begins, and update it directly. – Avi Nov 10 '15 at 11:34
  • Avi.. Have got the instance of the label its working fine ,,,But single character is read twice – S.s.Sailesh Kumar Nov 10 '15 at 11:58

2 Answers2

1

When you reload cell with

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

it will resignFirstResponder for your text field, and dismiss keyboard

Only solution is manually adjust cell height and table content height https://stackoverflow.com/a/33621733/1060785

Community
  • 1
  • 1
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
0

This Works for me fine

 - (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;

    }