开发者

Reload table in model class

开发者 https://www.devze.com 2023-04-02 04:55 出处:网络
I have one view class. In this class I have table view. In my model class I make Asynchrous ASIHTTPRequest. And I want when the operation is successful to reload data in table view. Which is good w开发

I have one view class. In this class I have table view. In my model class I make Asynchrous ASIHTTPRequest. And I want when the operation is successful to reload data in table view. Which is good w开发者_开发技巧ay to do this. I consider add one UITableView property to model class and use reload it. Is this good approach?


Make your model post notification to NSNotificationCenter and your table view register for this notification and reloadData when it receives one.

Inside your model, when changes occur:

[[NSNotificationCenter defaultCenter] postNotificationName:@"uniqueNotificationName"
                                                    object:self];

Inside your table view controller, register for notification, for example in viewDidAppear:

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

And implement methodToCall:

- (void)methodToCall:(NSNotification *)notification {
    [self.tableView reloadData];
}

When you are done with the table view, for example in viewWillDisappear: you need to unregister for notifications:

[[NSNotificationCenter defaultCenter] removeObserver:self];


I think your model should inform te controller that is has updated (or other way around using KVO). The controller should then message the view to reload.


When you want to reload table view you can use one of this approaches:

  1. If you want to reload all displayed cells then you can just call [tableView reloadData];. Notice, that only displayed cells will be reloaded. So if you have, for example, 10000 cells and only 10 of them are displayed, then only for 10 cells will be recalled method cellForRowAtIndexPath:.

  2. If you want to reload particular cells then you should call following method of UITableView : - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation and provide in first operand list of cells to reload.

  3. If you want to reload whole section (or number of sections) of UITableView then you should call - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation and provide in first operand list of sections to reload.

Hope, it will help you to choose appropriate variant.


use synchrous and gcd to load data. when receive data call [self reload] on main queue

0

精彩评论

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

关注公众号