开发者

Using KVO to reload data in UITableView

I've been expanding my horizons recently and am trying to start utilizing KVO more in my programming.

I have a view controller in my iPhone application which acts as the datasource and delegate for a UITableView. I also have a singleton model controller which coordinates populating my model with data fetched from the web.

In my view controller, I request that the model's controller load new data from the web. Then I can observe the "dataset" property of the singleton and receive KVO notifications when items are added to or removed from the set.

Now, each cell in my table view has an indicator which specifies whether the content in that cell has been read or not (like the blue "unread" dot in mail). Like mail, when a row is selected, I'll display details about that row. In the viewDidLoad for the detail view, I set the object's "read" property to YES. I would like the original view controller to be able to observe this "read" property of each ob开发者_StackOverflow中文版ject in the dataset, so that [tableView reloadData] can automatically be called as necessary and redraw the cells without the blue dot.

In researching this, I found the following link: http://homepage.mac.com/mmalc/CocoaExamples/controllers.html#observingACollection

According to this, it looks like I will do the following:

1) Be an observer of the array

2) Whenever I get a notification of a change to the array, I add (or remove) myself as an observer for the individual properties I am interested in.

3) When I get a notification of a change to the property I'm interested in, I can call [tableView reloadData]

I'm currently in the process of attempting to implement this approach. Can anyone with experience doing this offer some advice on this approach? If this the best way to handle this type of situation?

If this is the correct approach, would anyone be willing to share their implementation of adding/removing the observers for objects in the collection when the collection changes?

Thanks!


I think you can accomplish this task by using Core Data and the Fetched Results Controller. I'm sure this can save you a lot of work. Here's a good guide: Ray Wenderlich Core Data Tutorial, getting started

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜