第一遍 復雜方法 : 數據庫查詢一個表中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];
}
第三種: 完善后的方法 及賦值