2013-07-10 26 views
0

我有一個程序從Core Data中獲取,然後在程序中獲取某些東西時,它會在我的UITableView中顯示結果。現在如何實例化CD實體而不被提取

我的問題是,有時我需要用戶到另一個UITableCell添加到我UITableView但我tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section計數的元素在我的數組持有我的ExerciseData實體。

我試圖創建一個ExerciseData實體,就好像它是一個對象,但這不起作用並使程序崩潰。 做

if ([[_ed objectAtIndex:indexPath.row] weight]) { 
      NSLog(@"%@",_ed); 
      cell.weightInput.text = [NSString stringWithFormat:@"%@",[[_ed objectAtIndex:indexPath.row] weight]]; 
     } 

它拋出了讓我明白,我不能啓動這樣的一個CD實體錯誤CoreData: error: Failed to call designated initializer on NSManagedObject class 'ExerciseData'

然後我試圖添加到NSArray _ed一個NSString,然後檢查類,如果class type是一類NSString它不應該嘗試設置cell.weightInput.text

我的問題是2:1 )是有一種方法來啓動一個實體,以便我可以將它插入到數組中,然後檢查是否爲空,以便我可以在稍後的if statement上驗證它?

如果這是不可能的 2)我怎麼能填充NSArray的東西,然後可以驗證&升級到multimple項目,但也時有ExerciseData工作?

我的目標是,此人使用- addSet創造多次按下按鈕,然後我需要驗證無論是內部的一個新UITableCell這樣,如果是ExerciseData entity和性質權重設置爲填充cell.weightInput.text或以其他方式不填充它

- (void) addNewSet { 
    ExerciseData * newEd = [[ExerciseData alloc] init]; 
    NSMutableArray* ar = [[NSMutableArray alloc]initWithArray:_ed]; 
    [ar addObject:newEd]; 
    _ed = [[NSArray alloc] initWithArray:ar]; 
    [_mytable reloadData]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    workoutCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell = [[workoutCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; 
    } 

    cell.weightInput.text = @""; 
    if (indexPath.row < [_ed count]) { 
     if (![[[_ed objectAtIndex:indexPath.row] class] isKindOfClass:[NSString class]]) { 
      NSLog(@"%@",_ed); 
      cell.weightInput.text = [NSString stringWithFormat:@"%@",[[_ed objectAtIndex:indexPath.row] weight]]; 
     } 
    } 
//... 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([_ed count] == 0) { 
     return 1; 
    } else { 
     return [_ed count]; 
    } 
} 

回答

0

要創建一個新CoreData實體您需要:

ExerciseData *data = (ExerciseData*)[NSEntityDescription insertNewObjectForEntityForName:@"ExerciseData" inManagedObjectContext:yourContext]; 

確認後保存您的背景!

相關問題