0

I have 2 objects, lets call them super and sub, each super object is a standalone object, but each sub object must contain a property of that super object. Now i'm trying to configure a collection view

lets say I have 4 super objects and 70 sub objects, I want a section for each super object, and I want to display all 70 sub objects (since each sub object has its super property) I wanna organize those sub objects under their relevant super object

for now I have all sub objects into an array of something like 70 objects in count. but how do I organize everything and make it dynamic ? so that if i'll add more sub objects they will go straight under their correct super object section

basically need help configuring these:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
4

2 回答 2

0

您可以创建一个数组数组。

NSArray *subObjectArrays = [NSArray arrayWithObjects:subObjectArray1, subOjectArray2, subObjectArray3, subObjectArray4, nil];

其中subObjectArray1, subObjectArray2, subObjectArray3,subObjectArray4是每个超级对象的不同数组。

numberOfItemsInSection方法:

 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  return  [[subObjectArrays objectAtIndex:section] count];
}
于 2013-08-11T12:16:33.927 回答
0

基本上,您应该有一个数组数组,每个超级对象都有一个子数组。如果您需要为每个超级对象提供更多信息,您可以拥有一个字典数组,每个字典都包含一个包含所有子对象的数组。

然后,要配置集合视图,您可以这样做:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [superObjects count];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return [superObjects objectAtIndex:section] count];
}

如果您需要添加更多的超级或子对象,您可以将它们添加到数据结构中并调用reloadData集合视图。

于 2013-08-11T12:21:26.120 回答