Reputation: 2330
I'm having some code issues with updating a table view in ios. Let me explain my view structure first.
View A is the table view which is embedded in a navigation controller. View B and View C are both modal view controllers. The hierarchy is this.
A -> B -> C. When hitting "save" at C, this needs to close C and B return to A, reload the data in table A, and then push to navigation Controller D. With the table updated, whenever the user slides back to A, the changes from the modal views will show up.
Let me start from Modal View C right before the save.
self.workoutsVC.workoutName = workoutNameToSave;
NSArray *updatedWorkoutsArray = [databaseController getAllWorkouts];
NSMutableArray *updatedWorkouts =
[NSMutableArray arrayWithArray:updatedWorkoutsArray];
[self.workoutsVC receiveWorkoutNameAndDismissModalViews:workoutNameToSave
updatedWorkouts:updatedWorkouts];
Im maintaining a reference to View Controller A from C which I set up in the Segue earlier. Now let me show the next method which is back in View Controller A.
-(void) receiveWorkoutNameAndDismissModalViews:(NSString *)workoutNameParam
updatedWorkouts:(NSMutableArray *)updatedWorkouts
{
workoutArray = updatedWorkouts;
[_workoutTableView reloadData];
//Will dismiss modal views B and C before firing the push.This could be the problem
//but I'm not entirely positive if it is.
[self dismissViewControllerAnimated:YES completion:nil];
exercisesPerWorkoutViewController =
[self.storyboard
instantiateViewControllerWithIdentifier:@"ExercisesPerWorkout"];
exercisesPerWorkoutViewController.workoutName = workoutNameParam;
[self.navigationController
pushViewController:exercisesPerWorkoutViewController
animated:YES];
}
At this point the program will crash. updatedWorkouts will have 1 more workout which is the latest values after the user saved a workout. Essentially I'm just adding 1 new row to the workoutArray.
Here is the crash:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
I've tried different combinations of things. Commenting out the reloadData method will avoid the crash and Views B And C will dismiss and D will be pushed onto the stack but the data in A will not be updated. If I comment out the dismissViewControllers, then the reloadData method will function properly but B and C will not be dismissed. I've also tried doing reloadData on viewDidAppear or viewWillAppear but this too is causing the same crash as mentioned above. I'm pretty sure the problem is in the structure of my code but I do not know how to arrange this sequence and avoid this problem. If anyone could help me with this I'd really appreciate it.
**Also not sure if this could be a problem but I'll post how I'm referencing View Controller A from B and C.
B and C both contain properties to View Controller A as such:
@property (nonatomic, retain)Workouts *workoutsViewController;
So when View A goes to View B A -> B, B gets a reference to A as such
newWorkoutViewController = [segue destinationViewController];
newWorkoutViewController.workoutsViewController = self;
Then when View B goes to C B -> C C Gets B's reference to A as such:
randomWorkoutTypeSettingsViewController =
[segue destinationViewController];
randomWorkoutTypeSettingsViewController.exercises = exercises;
randomWorkoutTypeSettingsViewController.workoutsVC =
self.workoutsViewController;
Then finally, once user hits save and C closes and database updates are made then the initial code I posted earlier is hit starting the process:
self.workoutsVC.workoutName = workoutNameToSave;
NSArray *updatedWorkoutsArray = [databaseController getAllWorkouts];
NSMutableArray *updatedWorkouts =
[NSMutableArray arrayWithArray:updatedWorkoutsArray];
[self.workoutsVC receiveWorkoutNameAndDismissModalViews:workoutNameToSave
updatedWorkouts:updatedWorkouts];
However, I've done a similar approach for closing out a modal View Controller and then reloading data and pushing a new VC without any problems. I'm concerned because of how I'm closing two View Controllers and doing this might be causing the problem, but I'm not sure.
Upvotes: 0
Views: 217
Reputation: 5182
Try this in receiveWorkoutNameAndDismissModalViews:
,
[workoutArray setArray:updatedWorkouts];
[self dismissViewControllerAnimated:YES completion:^{
[_workoutTableView reloadData];
exercisesPerWorkoutViewController =
[self.storyboard
instantiateViewControllerWithIdentifier:@"ExercisesPerWorkout"];
exercisesPerWorkoutViewController.workoutName = workoutNameParam;
[self.navigationController
pushViewController:exercisesPerWorkoutViewController
animated:YES];
}];
Upvotes: 0
Reputation: 17585
From your error log, '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' which seems to be crash because of, you are trying to access first object in empty array.
Update:
You should see these two line with breakpoint..
1) This line lead to crash, if workoutArray
is empty.
Workout *tempPlaceholder = [workoutArray objectAtIndex:indexPath.row];
2) This line lead to crash if splitArray
have only one object(that's, if :
doesn't detect, it would have one object).
displayWorkoutName = splitArray[1];
Upvotes: 1