1

I have an UITableView that derives its data from an NSArray. In the same view as the UITableView, there is a UIDatePicker. When the 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?

Bill Shiff
  • 1,429
  • 2
  • 14
  • 17
  • 2
    Oh god why are you using the comma operator? *shudder* – Jonathan Grynspan Feb 07 '11 at 04:33
  • @Jonathan, And what's wrong with it? Even my hero Marcus Zarra uses the comma operator. See recently: http://www.cimgf.com/2010/06/05/re-ordering-nsfetchedresultscontroller/ – Neal L Feb 07 '11 at 04:40
  • @Neal: Appeal to authority. Munging two logical operations into a single statement leads to harder-to-read code. In the case of `[x release], x = nil` it's not so bad but it encourages worse behaviour down the line. See here: http://stackoverflow.com/questions/1613230/uses-of-c-comma-operator – Jonathan Grynspan Feb 07 '11 at 13:05

2 Answers2

2

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

Ishu
  • 12,797
  • 5
  • 35
  • 51
1

It seems you are not retaining availableUnits after releasing it. Try with

availableUnits = [[self availableUnitsForGivenTime] retain];
Marco Mustapic
  • 3,879
  • 1
  • 21
  • 20