在我的類中使用shouldChangeCharactersInRange來捕獲UITextField在uitableview中。shouldChangeCharactersInRange返回UITextField的第二個字符
問題是它沒有得到第一個輸入值並從第二個開始。
如果我輸入 「123」,它僅示出了 「12」
這是我的代碼:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
UITextField *utx = (UITextField *) textField;
UITableViewCell *cell = (UITableViewCell*)utx.superview.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
UITextField *utxtest = (UITextField *)[cell.contentView viewWithTag:20];
NSLog(@"utxtest %@", utxtest.text);
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
return YES;
}
,在我的tableview:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField * utxtest = (UITextField *)[cell.contentView viewWithTag:20];
utxtest = self;
return cell;
}
當你說,而不是'123'它顯示'23',那是什麼?在NSLog語句中還是在UITableViewCell中? – AMI289 2014-11-02 10:30:32
它在nslog輸出「utxtest 23」 – 2014-11-02 10:32:54
它顯示如果你NSLog的utx變量是什麼? – AMI289 2014-11-02 10:44:22