开发者

Maintaining data sync between UITableView cells and array of model objects

开发者 https://www.devze.com 2023-04-07 09:21 出处:网络
I have a UITableView where each cell corresponds to a model object.The list of these model objects are kept in an array inside a singleton object that manages the model objects. The UITableViewControl

I have a UITableView where each cell corresponds to a model object.The list of these model objects are kept in an array inside a singleton object that manages the model objects. The UITableViewController subc开发者_StackOverflow社区lass holds a instance variable that references this singleton object. The model objects update their internal data asynchronously from the web. What's the best method for updating the table cells when the corresponding model object finishes reloading its data? Should the model objects send out a notification? Can the table cells use KVO to receive changes from the model objects? Is there another option? What is the best practice here?


I found a solution by subclassing UITableViewCell. Each cell maintains a reference to the model object that it corresponds to and observes a boolean isLoading property of this object. When the loading state changes, the cell updates it's data. In other words, the cell (view) object observes the model object, then requests data to present upon the model object state changing.


I'm not sure that is the best solution but in a previous project I was calling the message reloadRowsAtIndexPaths:

NSArray* paths = [[NSArray alloc] initWithObjects:indexPath, nil];    
[tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationMiddle];
0

精彩评论

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

关注公众号