1

我的应用是货币计算器。每次文本字段更改时,ViewController 中的 TableView 都会重新加载数据。当 [[textField text] length] == 0 tableView 应该使用初始数据进行更新。不能共享整个代码,但它看起来像这样。

ViewController.m

- (void)viewDidLoad {
 [super viewDidLoad];
 array = [Model fromCoreData]; // initial data
}

- (void)textFieldDidChange:(id)textField {
 if([[textField text]length] == 0) {
   array = [Model fromCodeData]; //!
   [tableView reloadData]; // table gets same array that was when textfield length == 1
 }else{
   array = [Model calculate:array withSum:[textField text]]; //return array with calculated data
   [tableView reloadData];
 }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 //........
 //here [cell setBuy:[[array object atIndex:indexPath]buy]] .......... etc.
 //......
 return cell;
}

问题:当 [[textfield text] length] == 0 时,表获得与 [[textfield text] length] == 1 时相同的数组,但不是初始的(表应该从 coredata 中取回原始数据)。

我使用 MagicalRecord 来处理 CoreData。NSArray 包括实体。CoreData 没有实时变化。也许,有人面临同样的问题?有没有更合适的方法?对不起,我的英语不好 :)

4

1 回答 1

0

像这样尝试,你会成功。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *str= [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSLog(@"---%@",str);
    if([str length]==0){
        array = [Model fromCodeData]; //!
        [tableView reloadData];
    }
    else{
        array = [Model calculate:array withSum:[textField text]];
        [tableView reloadData];
    }

    return YES;
}
于 2013-07-19T05:15:02.433 回答