Hey everyone. I'm still getting the hang of Objective-C and I've been having trouble where my tableview is not showing data that is unique to the array that is created by an object of a main array.
I'm probably thinking it could be this line that is causing all data from all arrays of other objects to show up in the tableview:
Code:
//CardsViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
StudyCardsLiteAppDelegate *appDelegate = (StudyCardsLiteAppDelegate*)[[UIApplication sharedApplication] delegate];
return [[[appDelegate.deckArray objectAtIndex:appDelegate.deckLevel] deckList] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
StudyCardsLiteAppDelegate *appDelegate = (StudyCardsLiteAppDelegate*)[[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [[[[appDelegate.deckArray objectAtIndex:appDelegate.deckLevel] deckList] objectAtIndex:indexPath.row] cardTitle];
cell.detailTextLabel.text = [[[[appDelegate.deckArray objectAtIndex:appDelegate.deckLevel] deckList] objectAtIndex:indexPath.row] cardBody];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
or somewhere in here:
Code:
//CreateCardViewController.m
-(IBAction) makeCard:(id)sender {
NSLog(@"adding new card");
StudyCardsLiteAppDelegate *appDelegate = (StudyCardsLiteAppDelegate*)[[UIApplication sharedApplication] delegate];
Cards *newCard = [[Cards alloc] init];
[newCard setCardTitle:cardTitleText.text]; NSLog(@"%@", newCard.cardTitle);
[newCard setCardBody:cardBodyText.text]; NSLog(@"%@", newCard.cardBody);
[[[appDelegate.deckArray objectAtIndex:appDelegate.deckLevel] deckList] insertObject:newCard atIndex:[[[appDelegate.deckArray objectAtIndex:appDelegate.deckLevel] deckList] count]];
[newCard release];
[cvController.newTableView reloadData];
cardTitleText.text = @"";
cardBodyText.text = @"";
createButton.enabled = NO;
[self dismissModalViewControllerAnimated:YES];
}
where deckArray is the main NSMutableArray that is declared in the AppDelegate and deckLevel is an NSInteger in the AppDelegate that is set to indexPath.row in the previous viewcontroller where I make the deck object (contains deck title as a string and deckList as an NSMutableArray). I am pretty sure the data is being saved to their respective arrays, because when I try to delete a row in the tableview that belongs to another deck, the App crashes in the simulator, meaning it does not exist in that array as well.
I have been trying to figure out what is wrong for hours and still can't find what is causing this. What could the problem be?