开发者

UITableView tied to a NSArray

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜