zoukankan      html  css  js  c++  java
  • UICollectionView 数据库元素分组 多种section分开显示

    第一遍 复杂方法 : 数据库查询一个表中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];

    }

     

     第三种: 完善后的方法 及赋值 

  • 相关阅读:
    独家干货!两种低代码模型驱动开发技术对比
    GeneXus低代码产品版本大更新,助力打造专业开发团队,加速企业数字化转型!
    企业级低代码平台应具备哪些要素?(下)
    如何选择低代码平台?GeneXus低代码平台
    C语言结构体的“继承”
    C语言实现wake on lan(网络唤醒)
    Linux驱动中的异步通知
    Linux网络设备驱动概述
    Linux下的广播程序
    DCMA86P2网卡成功显示802.11p
  • 原文地址:https://www.cnblogs.com/tangyuanby2/p/7218825.html
Copyright © 2011-2022 走看看