Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 8:最佳方法:主细节格式的多个UICollectionView&;单例数据源_Ios_Ios8_Uicollectionview_Datasource - Fatal编程技术网

iOS 8:最佳方法:主细节格式的多个UICollectionView&;单例数据源

iOS 8:最佳方法:主细节格式的多个UICollectionView&;单例数据源,ios,ios8,uicollectionview,datasource,Ios,Ios8,Uicollectionview,Datasource,我在iPad应用程序的主细节布局中有两个UICollectionViewSC1和C2。C1是主采集视图,显示主数据。当点击C1中的单元格时,C2将被更新并显示所有对象信息,包括C1上已显示的主要对象信息 两个集合视图是否都可以有一个数据源? 我在下面给出了数据源的设计 @protocol MyDataSourceDelegate <NSObject> @optional //Below method is called to notify collection view that

我在iPad应用程序的主细节布局中有两个
UICollectionView
SC1和C2。C1是主采集视图,显示主数据。当点击C1中的单元格时,C2将被更新并显示所有对象信息,包括C1上已显示的主要对象信息

两个集合视图是否都可以有一个数据源? 我在下面给出了数据源的设计

@protocol MyDataSourceDelegate <NSObject>

@optional
//Below method is called to notify collection view that a new object is added to the data source and so it must update its UI
- (void)didInsertItemAtIndexPath:(NSIndexPath*)indexPath;

//Below method is called to notify collection view that an existing object is removed from the data source and so it must update its UI
- (void)didRemoveItemAtIndexPath:(NSIndexPath*)indexPath;

@end

@interface MyDataSource : NSObject

@property(nonatomic,strong)NSArray *fetchedObjects;//main objects array
@property(nonatomic,strong)NSArray *masterObjects;//array of objects displayed in master collectionview
@property(nonatomic,strong)NSArray *detailObjects;//array of objects displayed in detail collection view in multiple sections.

//called from within collection view when a cell is tapped
- (void)selectItemAtIndexPath:(NSIndexPath*)indexPath isMasterData:(BOOL)isMaster;

//call when a collectionview cell needs information for a particular cell's contents
- (id)itemAtIndexPath:(NSIndexPath*)indexPath isMasterData:(BOOL)isMaster;
- (id)itemAtSection:(NSInteger)section isMasterData:(BOOL)isMaster;

@end
@protocol MyDataSourceDelegate
@可选的
//调用下面的方法来通知集合视图一个新对象已添加到数据源,因此它必须更新其UI
-(void)didInsertItemAtIndexPath:(NSIndexPath*)indexPath;
//调用Below方法以通知集合视图现有对象已从数据源中删除,因此它必须更新其UI
-(void)didremovietematindexpath:(nsindepath*)indepath;
@结束
@接口MyDataSource:NSObject
@属性(非原子,强)NSArray*获取的对象//主对象数组
@属性(非原子、强)NSArray*主对象//主集合视图中显示的对象数组
@属性(非原子,强)NSArray*detailObjects//显示在多个分区中的详细信息集合视图中的对象数组。
//点击单元格时从集合视图内调用
-(void)selectItemAtIndexPath:(NSIndexPath*)indexPath isMasterData:(BOOL)isMaster;
//当collectionview单元格需要特定单元格内容的信息时调用
-(id)itemAtIndexPath:(NSIndexPath*)indexPath isMasterData:(BOOL)isMaster;
-(id)itemAtSection:(NSInteger)section isMaster数据:(BOOL)isMaster;
@结束

上述实现的问题是,我希望将与数据操作相关的工作完全排除在承载上述两个集合视图的视图控制器之外,以避免两个集合视图的膨胀和相同的数据源。在上述场景中,
collectionviews
MyDataSourceDelegate
协议之间的委托将如何工作?据我所知,委托关系是1-1关系,因此如何在两个集合视图之间管理单个数据源?

数据源是在应用程序中传递信息的一种非常方便的方法,可以处理您选择在项目中实现的几乎每个类。我主要使用数据源来处理核心数据模型,但我也将其用于其他事情(传递自定义方法等)

我最近为两个tableview控制器使用了一个数据源,这确实导致了一些问题。但是,如果在每个集合视图的数据源中保持数据的独立性,则应该没有问题。当您开始在控制器和数据源之间传递属性(尤其是数组)时,它会变得很麻烦。您可能会面临数据突变的风险

我的一位老师曾经说过,最好的程序员写的代码最少。但是,如果您发现数据发生了变化,或者没有得到所需的结果,您可能只需要暂时在控制器中实现它。让它工作,然后决定将其移动到数据源的最佳方式。您还可以考虑为每个控制器中的数组创建类似的属性,以存储来自数据源的信息。可以使用不可变数组,但如果要处理数据,则需要将其存储在新属性中

考虑下面的场景。我将使用*fetchedObject数组作为示例

数据源.h 考虑创建为每个集合视图指定的单独实例

DataSource.m 现在,您有两个使用相同数据填充的独立实例。一个用于C1,一个用于C2。这就是你在另一个班级里对他们的称呼

[DataSource sharedInstance].fetchedObjectsC1;
[DataSource sharedInstance].fetchedObjectsC2;
考虑这样一个场景:您试图使用来自数据源的相同NSArray实例填充两个单独的TableView/集合视图。当您开始实现caneditrowatinexpath:和其他操作表视图数据的方法时,您还将影响其他集合视图的功能。尤其是在整个过程中保存/获取核心数据时。这通常会导致崩溃

根据我掌握的信息,这是我能做的最好的,如果这对你不起作用,请告诉我,我们会解决其他问题。不要忘记将您的单例添加到数据源中,并确保它是公共的

+(instancetype) sharedInstance{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    
    return sharedInstance;
}
- (void) fetchCoreData{
     //set up fetch
     self.fetchedObjectsC1 = [NSArray arrayWithArray:[self.fetchedResultsController fetchedObjects]];
     self.fetchedObjectsC2 = [NSArray arrayWithArray:[self.fetchedResultsController fetchedObjects]];

}
[DataSource sharedInstance].fetchedObjectsC1;
[DataSource sharedInstance].fetchedObjectsC2;