I have an UITableView
that derives its data from an NSArray
. In the same view as the UITableView
, there is a UIDatePicker
. When th开发者_如何学运维e date in the UIDatePicker
changes, then I need to update the NSArray
that provides data to the the UITableView
and reload the table.
The problem is that I can't figure out how to change the NSArray
without crashing my app. I think that the tableview is tied to the NSArray and if I try to release it then that's what causes the crash.
Here's the method that is called when the UIDatePicker
's date is changed:
- (IBAction)datesChanged {
// Update the availableUnits NSArray
if (availableUnits != nil) {
[availableUnits release], availableUnits = nil;
}
availableUnits = [self availableUnitsForGivenTime];
[self.tableView reloadData];
}
What am I missing here?
Use NSMutableArray insted NSArray and each time when you change date then first remove old date from Array.Declare NSMutable array as property and alloc it in ViewDidLoad.then.
- (IBAction)datesChanged {
[self.availableUnits removeAllObjects];
self.availableUnits = [[self availableUnitsForGivenTime] retain];
[self.tableView reloadData];
}
And release the array in dealloc
It seems you are not retaining availableUnits
after releasing it. Try with
availableUnits = [[self availableUnitsForGivenTime] retain];
精彩评论