从tableview单元格更新文本并刷新

时间:2015-11-10 11:25:42

标签: ios objective-c uitableview uitextfield

您好我在自定义视图中有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;
}

2 个答案:

答案 0 :(得分:0)

使用

重新加载单元格时

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

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

仅解决方案是手动调整单元格高度和表格内容高度https://stackoverflow.com/a/33621733/1060785

答案 1 :(得分:0)

这对我来说很好

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

    }