第一遍 复杂方法 : 数据库查询一个表中userID 然后进行分类 中间去重 获得ID个数 放到section 中 显示
#pragma mark - 查询不同的ID 各数
- (void)checkIDsWithCount{
RLMRealm * realm = [[RealmConfigurationshared]realm];
// RLMResults * selectIDResults = [[CarVideoInfoObj objectsInRealm:realm where:@"userID !=0"] sortedResultsUsingKeyPath:@"hostID" ascending:NO];
RLMResults * selectIDResults = [[CarVideoInfoObj allObjectsInRealm:realm] sortedResultsUsingKeyPath:@"hostID"ascending:NO];
NSLog(@"selectIDResults is %@",selectIDResults);
NSMutableArray *userID = [NSMutableArrayarray];
allSectionDic = [NSMutableDictionarynew];
for(CarVideoInfoObj * obj in selectIDResults){
NSInteger userId = obj.userID;
NSNumber * key = @(userId);
NSMutableArray * objects = [allSectionDicobjectForKey:key];
if(objects == nil){
objects = [NSMutableArraynew];
[allSectionDicsetObject:objects forKey:key];
}
[objects addObject:obj];
// NSLog(@"objectID==== %ld",(long)userId);
// [userID addObject:@(userId)];
}
NSLog(@"all carVideoInfo object list is %@",allSectionDic);
// 去重查询个数
NSLog(@"%lu",(unsignedlong)allSectionDic.count);
//统计数组相同元素的个数
sectionDic = [[NSMutableDictionaryalloc] init];
set = [NSSetsetWithArray:userID];
for (NSString *setString inset){
//需要去掉的元素数组
NSMutableArray *filteredArray = [[NSMutableArrayalloc]initWithObjects:setString, nil];
NSMutableArray *dataArray = userID;
NSPredicate * filterPredicate = [NSPredicatepredicateWithFormat:@"NOT (SELF IN %@)",filteredArray];
//过滤数组
NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];
NSLog(@"Reslut Filtered Array = %@",reslutFilteredArray);
int number = (int)(dataArray.count-reslutFilteredArray.count);
NSLog(@"number :%d",number);
[sectionDicsetObject:[NSStringstringWithFormat:@"%d",number] forKey:setString];
}
NSLog(@"dic is%@", sectionDic);
_userIDdic = [[NSDictionaryalloc]init];
_userIDdic = sectionDic;
NSArray * keys = [_userIDdicallKeys] ;
dicArray =[NSArrayarray];
dicArray = keys;
}
#pragma mark - UICollectionView delegate dataSource
#pragma mark 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
// section 个数
returnset.count;
}
#pragma mark 定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// sectionDic is{
// 10006 = 14;
// 10003 = 23;
// }
NSString *key = [dicArrayobjectAtIndex:section];
NSString * count = [_userIDdicobjectForKey:key];
return count.integerValue;
// return 2;
// NSLog(@"2222_videoMode%@",self.videoMode);
// NSLog(@"_videoMode.videos count is %lu",(unsigned long)[self.videoMode.carVideoInfoObjArray count]);
// return [self.videoMode.carVideoInfoObjArray count];
}
第二种方法 查询表格 讲userID 设置为字典的key 不进获取到了以userID不同的个数作为 section 而且每个 userI获取了每个userID的数据 为赋值显示 打好了基础 (仍不完善)
#pragma mark - 查询不同的ID 各数
- (void)checkIDsWithCount{
RLMRealm * realm = [[RealmConfigurationshared]realm];
RLMResults * selectIDResults = [[CarVideoInfoObj allObjectsInRealm:realm] sortedResultsUsingKeyPath:@"hostID"ascending:NO];
NSLog(@"selectIDResults is %@",selectIDResults);
allSectionDic = [NSMutableDictionarynew];
for(CarVideoInfoObj * obj in selectIDResults){
NSInteger userId = obj.userID;
NSNumber * key = @(userId);
NSMutableArray * objects = [allSectionDicobjectForKey:key];
if(objects == nil){
objects = [NSMutableArraynew];
[allSectionDicsetObject:objects forKey:key];
}
[objects addObject:obj];
}
NSLog(@"all carVideoInfo object list is %@",allSectionDic);
_userIDdic = [[NSDictionaryalloc]init];
_userIDdic = allSectionDic;
NSArray * keys = [_userIDdicallKeys] ;
dicArray =[NSArrayarray];
dicArray = keys;
}
#pragma mark - UICollectionView delegate dataSource
#pragma mark 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
// section 个数
returnallSectionDic.count;
}
#pragma mark 定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSString *key = [dicArrayobjectAtIndex:section];
// NSString * count = [_userIDdic objectForKey:key];
return [[_userIDdicobjectForKey:key] count];
}
第三种: 完善后的方法 及赋值