简体   繁体   English

UITableViewController和一个单独的数据源对象

[英]UITableViewController with a separate data source object

I have a separate object for the data source of a UITableViewController. 我有一个单独的对象,用于UITableViewController的数据源。 I want to modify the data source dynamically but I don't want to have a direct reference of the tableView object inside the data source. 我想动态地修改数据源,但是我不想在数据源内部直接引用tableView对象。 How can I notify the controller about the changes in the data source? 如何通知控制器有关数据源的更改?

Currently I use the notification center but I don't believe this is the right choice. 目前,我使用通知中心,但我不认为这是正确的选择。

可以发送NSNotification通知UITableView模型已更改,可以再次读取数据并重新加载表。

It would be appropriate to give the data source a reference to the table view to be refreshed or to add a property which is a block that the data source calls when some data has been updated (this block is supplied by the view controller and just reloads the table view). 给数据源提供对要刷新的表视图的引用,或者添加一个属性,该属性是数据源在某些数据已更新时调用的块(该块由视图控制器提供并重新加载)将是适当的。表格视图)。

The block approach is a little more generic and allows your data source to more easily be used with table / collection views. 块方法更具通用性,可让您的数据源更轻松地用于表/集合视图。

In both cases, the relationship you're looking at is direct and 1 to 1. This is not a suitable place to use notifications. 在两种情况下,您正在查看的关系都是直接的,并且是1对1。这不是使用通知的合适位置。 Notifications are for non-direct, broadcast type 1 to many communication requirements. 通知针对许多通信需求的非直接广播类型1。

I got struct at the thing, One alternative I fount was KVO but I did not get much success implementing KVO, So I am also thinking to use Block. 我对结构很了解,我找到的一种替代方法是KVO,但是我在实现KVO方面没有获得太多成功,因此我也正在考虑使用Block。 And you preferred which approach ? 您首选哪种方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM