2014-09-11 5 views
0

내 앱에는 사용자가 자신에게 알레르기가있는 성분 표보기를 만들 수있는 기능이 있습니다. 다음은 방법뿐만 아니라 웁니다 배열을 만들기위한 나의 방법은 그것을이 채울 :핵심 데이터 SIGABRT 데이터를 가져 오는 중?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    badIngredientsArray = [[NSMutableArray alloc] init]; 
    rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" 
               style:UIBarButtonItemStyleDone target:nil action:nil]; 
    self.navigationItem.rightBarButtonItem = rightButton; 

    rightButton.target = self; 
    rightButton.action = @selector(addRow); 

    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AllergicIngredient" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 

    NSError *error; 

    NSArray *ingredientsArray = [context executeFetchRequest:fetchRequest error:&error]; 
    badIngredientsArray = [NSMutableArray arrayWithArray:ingredientsArray]; 
} 

-(void)addRow 
{ 
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Add a Bad Ingredient" message:@"Type the name of the ingredient" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil]; 
    myAlertView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [myAlertView show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    AllergicIngredient *allergic = [NSEntityDescription insertNewObjectForEntityForName:@"AllergicIngredient" inManagedObjectContext:context]; 

    NSString *enteredString = [[alertView textFieldAtIndex:0] text]; 

    [allergic setValue:enteredString forKey:@"name"]; 

    NSError *error; 

    if (![context save:&error]) 
    { 
     NSLog(@"Couldnt find the save %@", error.localizedDescription); 
    }  
    else 
    { 
     NSLog(@"It saved properly"); 
    } 

    [badIngredientsArray addObject:enteredString]; 
    [self.tableView reloadData]; 
} 

것 같다 나는 다음과 같은 코드를 제거 할 때 마치 :

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSManagedObjectContext *context = [self managedObjectContext]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AllergicIngredient" inManagedObjectContext:context]; 
[fetchRequest setEntity:entity]; 

NSError *error; 

NSArray *ingredientsArray = [context executeFetchRequest:fetchRequest error:&error]; 
badIngredientsArray = [NSMutableArray arrayWithArray:ingredientsArray]; 

기능을 잘 작동하지만 애플 리케이션을 삭제하고 다시 실행하면 테이블 뷰가 비어있는 핵심 데이터 측면이 없어집니다. 그래서 위의 코드에 무엇이 잘못되었는지 주위를 감싸는 것처럼 보일 수 없습니다.

cell.textLabel.text = [badIngredientsArray objectAtIndex:indexPath.row]; 

UPDATE :

내 테이블 뷰 방법 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [badIngredientsArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    if (cell == nil) 
{ 
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
} 

cell.textLabel.text = [badIngredientsArray objectAtIndex:indexPath.row]; 

return cell; 
} 
+0

하시기 바랍니다가 우리에게'있는 tableView을 보여','있는 tableView : numberOfRowsInSection :'와'numberOfSectionsInTableView을'. –

+0

주 스레드에서 핵심 데이터 가져 오기를 수행하고 있습니다. 이것은 나쁘다. 또한 쓰레드 감금을 사용하고 있으며 이는 쓸모가 없습니다. 개인 대기열 컨텍스트와 함께 대기열 감금을 사용하면 훨씬 행복해집니다. – quellish

답변

1

[badIngredientsArray objectAtIndex:indexPath.row]는 반환하지 않습니다 SIGABRT 오류가 발생하는 위치를 알고 자하는 여러분 모두를 위해,이 라인에서의 NSString이지만 NSManagedObject입니다.

tableView:cellForRowAtIndexPath:, 귀하의 name 속성을 얻기 위해 다음 코드를 시도 : cellForRowAtIndexPath :

AllergicIngredient *object = (AllergicIngredient *)[badIngredientsArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = object.name; 
//or 
//NSManagedObject *object = (NSManagedObject *)[badIngredientsArray objectAtIndex:indexPath.row]; 
//cell.textLabel.text = [object objectForKey:@"name"]; 
관련 문제