美文网首页
IOS 把model数组中相同元素放到tableView的同一个

IOS 把model数组中相同元素放到tableView的同一个

作者: Spring_Lau | 来源:发表于2016-07-15 08:43 被阅读391次

    NSArray *dataArray = responseObject[@"data"];

    [self . dateArray removeAllObjects];

    [self.allArray removeAllObjects];

    if (self.isLoading == NO) {

    [self.allArray removeAllObjects];

    }

    for (NSDictionary *dic in dataArray) {

    GoOutModel *model = [[GoOutModel alloc] init];

    [model setValuesForKeysWithDictionary:dic];

    NSArray * arra = [dic[@"publishTime"] componentsSeparatedByString:@" "];

    NSString * str = arra[0];

    [self.dateArray addObject:str];

    [self.allArray addObject:model];

    }

    /////////关键字数组

    NSMutableArray * tmpArray = [NSMutableArray array];

    self . tmpDic = [NSMutableDictionary dictionary];

    NSMutableArray * tmpArr2 =[[NSMutableArray alloc]initWithArray:self.dateArray];

    //--------------这是用来保存model的数组

    NSMutableArray * tmpArray00 = [NSMutableArray array];

    NSMutableArray * tmpArr200 =[[NSMutableArray alloc]initWithArray:self.allArray];

    for (int i =0;i<tmpArr2.count;i++) {

    NSString * key =  tmpArr2[i];

    /*

    1.先对model里的关键字进行读取

    2.看关键字是否在数组中存在

    3.如果不存在就新创建一个数组 把关键字对应的i值所对应的model添加到数组里

    如果存在就获取到key所在的数组 并把新的相同关键字的model添加进去

    4.👌了

    */

    if (![tmpArray containsObject:self . dateArray[i]]) {

    NSMutableArray *mutableArray = [[NSMutableArray alloc]init];

    [tmpArray addObject:self . dateArray[i]];

    //                [mutableArray addObject:self . dateArray[i]];

    //                [tmpDic setValue:mutableArray forKey:key];

    //---------

    [tmpArray00 addObject:self . allArray[i]];

    [mutableArray addObject:self . allArray[i]];

    [self . tmpDic setValue:mutableArray forKey:key];

    }else{

    //数组已经存在需要存数据

    NSMutableArray * tmpArray00 = self . tmpDic[key];

    [tmpArray00 addObject:tmpArr200[i]];

    }

    }

    NSLog(@"-----tmpDic----%@,%lu",self . tmpDic,(unsigned long)self . tmpDic.allKeys.count);

    //因为字典是无序的 所以要对字典里的键进行排序 使用排序后的key数组

    dicArr = self . tmpDic.allKeys;

    dicArr = [dicArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){

    NSComparisonResult result = [obj2 compare:obj1];

    return result==NSOrderedDescending;

    }];

    相关文章

      网友评论

          本文标题:IOS 把model数组中相同元素放到tableView的同一个

          本文链接:https://www.haomeiwen.com/subject/pgkyjttx.html