単純なマスター/詳細アプリケーションがあり、詳細ビューを使用してマスター リストに挿入するデータを構成しています。
「Calculation」というカスタム クラスがあります。これは、各行に挿入する必要があるオブジェクトです。
オブジェクトを詳細ビューからマスター ビューに渡すことはできますが、新しい行は挿入されません。
詳細ビューで、オブジェクトを開始し、データを入力して、カスタム セグエを使用して MasterViewController の「insertNewObject」メソッドに送信します。
- (IBAction)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *s = @"1";
NSNumber *n = [f numberFromString:s];
Calculation *calc = [[Calculation alloc] initWithValue:@"$1661.70" descr:@"1 Ounce 24k Gold" textInput:@"1" percentDisplay:@"0%" purityIndex:n metalIndex:n weightUnitIndex:n sliderValue:n];
[segue.destinationViewController insertNewObject:calc];
}
MasterViewController には、挿入コードがあります。
-(void)insertNewObject:(NSObject *)newCalculation {
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:newCalculation atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSObject *object = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = [object valueForKey:@"value"];
cell.detailTextLabel.text = [object valueForKey:@"descr"];
cell.textLabel.shadowColor = [UIColor blackColor];
cell.textLabel.shadowOffset = CGSizeMake(0, -1);
return cell;
}
MVC で NSLog を使用してオブジェクトを終日印刷できますが、詳細ビュー コントローラーからオブジェクトを何度追加しても、配列カウントは常に 1 であり、テーブルビューにセルが追加されることはありません。
MVC でオブジェクトをインスタンス化して挿入すると、まったく同じコードで問題なく動作します。