开发者

Automatic/timed reload of tableView from model?

I am working on an application which uses a singleton data model and two controllers (one a mapView and the other a tableView) both configured using a UITabBarController. I have setup a delegate on the mapView to acquire CLLocation data and add it to an NSMutableArray in the data model. I want to add this data as cells to the tableView, I have accesses to the data via the shared model so that is not a problem. My question is:

(1) is there a way to automatically update the tableView as new data goes into the model (NSMutableArray)?

EDIT:

I have just been thinking about this and it looks to me like I should be using the model as the开发者_StackOverflow中文版 delegate for the CLLoctionManager, storing the data in the NSMutableArray as I detailed above. That way both the MapView and the TableView have access to the data. Can anyone help me with reloading the tableView, is there an automatic / timer way to do that?


You could do it by registering your view that contains the tableView for notifications.

 [[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(yourSelector:) 
                                      name:@"addedData" 
                                      object:nil];

Then implement your selector:

- (void)yourSelector:(NSNotification *)notification {
        [self.tableView reloadData];
    //do something
}

Now in the class where the data is added, you post a notification that new data was added.

[[NSNotificationCenter defaultCenter] postNotificationName:@"addedData" 
                                      object:nil];


First off, there's a reloadData method on a UITableView which you can use to force it to reload its data.

However as a suggestion, rather than using a timer why not implement your own protocol/delegate as a part of your model class. (Here's a pretty good tutorial on using custom delegates in Objective-C, although from memory I think it omits a [someObject setDelegate:self] somewhere along the way.)

You could make the view controller that's responsible for the table view a delegate for your model, which would inform the view controller that the underlying data has changed and that a reload is required. The view controller would then presumably inform the user (spinning wheel 'o' happy, etc.) and call reloadData on the table as required.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜