美文网首页iOS开发
后台返回一组数据(一个数组), 然后按照日期进行分组, 用UIT

后台返回一组数据(一个数组), 然后按照日期进行分组, 用UIT

作者: 努力奔跑的小男孩 | 来源:发表于2018-01-02 17:06 被阅读20次
    数组按照日期分组.gif
    /**字典的key*/
    static NSString *DicKey = @"dateKey";
    /**字典的value*/
    static NSString *DicValue = @"dateArr";
    /**tableView的section是否选中 @(1) 选中 @(0) 不选中*/
    static NSString *SectionSelect = @"sectionSelect";
    
    用到的model

    .h文件

    #import <Foundation/Foundation.h>
    @interface CollectionWordModel : NSObject
    /**单词id*/
    @property (nonatomic, strong) NSString *wordId;
    /**单词名称*/
    @property (nonatomic, strong) NSString *name;
    /**添加时间*/
    @property (nonatomic, strong) NSString *createDate;
    /**状态 normal(正常状态) selected(选中状态) unselected(非选中状态)*/
    @property (nonatomic, strong) NSString *stateStr; // 自己定义的,类似点赞
    @end
    

    .m文件

    #import "CollectionWordModel.h"
    @implementation CollectionWordModel
    @end
    
    分组方法
    #pragma mark - 按日期分组
    - (NSArray *)sortArr:(NSArray *)dataArray{
        // 首先把原数组中数据的日期取出来放入dateStrArr
        NSMutableArray *dateStrArr = [NSMutableArray array];
        [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CollectionWordModel *model = obj;
            [dateStrArr addObject:model.createDate];
        }];
        // 使用asset把timeArr的日期去重
        NSSet *set = [NSSet setWithArray:dateStrArr];
        NSArray *userArray = [set allObjects];
        NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];//yes升序排列,no,降序排列
        //按日期降序排列的日期数组
        NSArray *sortDateArr = [userArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];
        //此时得到的sortDateArr就是按照时间降序排列拍好的数组
        NSMutableArray *sortDataArray = [NSMutableArray array];
        [sortDateArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSMutableArray *arr = [NSMutableArray array];
            NSDictionary *dic = @{DicKey:(NSString *)obj,DicValue:arr,SectionSelect:@(0)};
            [sortDataArray addObject:dic]; // 每一个日期都是一个字典
        }];
        
        
        
        //遍历_dataArray取其中每个数据的日期看看与myary里的那个日期匹配就把这个数据装到_titleArray 对应的组中
        [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CollectionWordModel *model = obj;
            for (NSString *eachDateStr in sortDateArr) {
                if ([model.createDate isEqualToString: eachDateStr]) {
                    //NSLog(@"%zd",[dateStrArr indexOfObject:eachDateStr]);
                    NSDictionary *dic = [sortDataArray objectAtIndex:[sortDateArr indexOfObject:eachDateStr]];
                    NSMutableArray *array = dic[DicValue];
                    [array addObject:model];
                }
            }
        }];
        return sortDataArray;
    }
    

    详细代码

    //
    //  CollectionWordItem.m
    //  SparkWordNew
    //
    //  Created by 万艳勇 on 2017/12/30.
    //  Copyright © 2017年 李凌飞. All rights reserved.
    //
    
    #import "CollectionWordItem.h"
    #import "NoContentTipView.h" // 没有内容提示
    #import "WordBookNormalCell.h" // cell
    #import "WrongWordHeaderSectionView.h" // headerView
    #import "WrongWordFooterSectionView.h" // footerView
    #import "CollectionWordModel.h" // Model
    #import "CustomAlertView.h" // 提示框
    
    static NSString *DicKey = @"dateKey";
    static NSString *DicValue = @"dateArr";
    static NSString *SectionSelect = @"sectionSelect";
    
    static NSString *collectionWordCellID = @"collectionCellID";
    static NSString *sectionHeaderView = @"sectionHeaderViewID";
    @interface CollectionWordItem() <UITableViewDelegate, UITableViewDataSource>
    @property (nonatomic, strong) UITableView *collectionWordTableView;
    /**收藏单词数据*/
    @property (nonatomic, strong) NSMutableArray *collectionWordArr;
    /**用于提示*/
    @property (nonatomic, strong) NoContentTipView *tipView;
    /**去复习*/
    @property (nonatomic, strong) UIButton *reviewBtn;
    /**底部用于显示 全选 删除按钮视图*/
    @property (nonatomic, strong)  UIView *bottomView;
    /**全选按钮*/
    @property (nonatomic, strong) UIButton *selectedAllBtn;
    
    /**AlertView*/
    @property (nonatomic, strong) CustomAlertView *alertView;
    
    /**保存要删除的数组*/
    @property (nonatomic, strong) NSMutableArray *wantDeleteDataArr;
    
    /**是否是删除状态 默认是false 正常状态 treu 是删除状态*/
    @property (nonatomic, assign) BOOL deleteFlag;
    
    @end
    
    @implementation CollectionWordItem
    
    #pragma mark - 懒加载
    - (NoContentTipView *)tipView{
        if (!_tipView) {
            NoContentTipView *tipView = [[NoContentTipView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64 - 43)];
            [tipView initWithImageName:@"NoContentImage" tipText:@"目前还没有错误的单词呢!"];
            _tipView = tipView;
            tipView.hidden = true;
            [self.contentView addSubview:tipView];
        }
        return _tipView;
    }
    - (NSMutableArray *)collectionWordArr{
        if(!_collectionWordArr){
            _collectionWordArr = [NSMutableArray arrayWithCapacity:10];
        }
        return _collectionWordArr;
    }
    
    
    - (CustomAlertView *)alertView{
        if(!_alertView){
            _alertView = [[CustomAlertView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
            [_alertView initWithTitleStr:@"确定要删除所选单词吗?" Target:self cancelAction:@selector(alertCancelAction) OKAction:@selector(alertOKAction)];
            _alertView.hidden = true;
            UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
            
            [keyWindow addSubview:_alertView];
        }
        return _alertView;
    }
    
    - (NSMutableArray *)wantDeleteDataArr{
        if (!_wantDeleteDataArr) {
            _wantDeleteDataArr = [NSMutableArray arrayWithCapacity:10];
        }
        return _wantDeleteDataArr;
    }
    
    
    - (instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            self.contentView.backgroundColor = [UIColor colorFromHexRGB:@"f2f2f2"];
            _deleteFlag = false; // 默认是 正常状态
            [self setUpSubviews];
            //[self loadData];
            
            NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:10];
            for (int i = 0; i < 10 ; i ++) {
                CollectionWordModel *model = [[CollectionWordModel alloc]init];
                model.wordId = [NSString stringWithFormat:@"%d",i];
                model.name = [NSString stringWithFormat:@"spark%d",i];
                model.createDate = [NSString stringWithFormat:@"2018-%d",i];
                [tempArr addObject:model];
            }
            for (int i = 0; i < 30 ; i ++) {
                CollectionWordModel *model = [[CollectionWordModel alloc]init];
                model.wordId = [NSString stringWithFormat:@"%d",i];
                model.name = [NSString stringWithFormat:@"spark%d",i];
                model.createDate = [NSString stringWithFormat:@"2018-%d",i];
                [tempArr addObject:model];
            }
            self.collectionWordArr = [NSMutableArray arrayWithArray:[self sortArr:tempArr]];
        }
        return self;
    }
    
    - (void)loadData{
        [[XHNetWorking shareNetWorking] getRequestWithURL:wrongWordsList parameters:nil successBlock:^(id  _Nullable responseObject, NSURLSessionDataTask * _Nullable task) {
            NSArray *resArr = (NSArray *)responseObject;
            if (resArr.count == 0) {
                [self.tipView initWithImageName:@"NoContentImage" tipText:@"目前还没有错误的单词呢!"];
                self.tipView.hidden = false;
                [self.contentView bringSubviewToFront:self.tipView];
                //
            }else{ // 解析数据 , 刷新列表
                self.tipView.hidden = true;
                [self.contentView sendSubviewToBack:self.tipView];
                
                self.collectionWordArr = [CollectionWordModel mj_objectArrayWithKeyValuesArray:resArr];
                
                //            [WrongWordModel mj_setupObjectClassInArray:^NSDictionary *{
                //                return @{
                //                         @"questionList" : [QuestionListModel class],
                //                         };
                //            }];
                //
                //            for (NSDictionary *dic in resArr) {
                //                WrongWordModel*model = [WrongWordModel mj_objectWithKeyValues:dic];
                //                [self.wrongWordArr addObject:model];
                //                [self.wrongWordTableView reloadData];
                //            }
            }
            NSLog(@"错词本responseObject:%@",responseObject);
            
        } faildBlock:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nullable task) {
            
            NSLog(@"错词本error:%@",[commonTools returnErrorMessageWithError:error]);
        }];
    }
    
    
    - (void)setUpSubviews{
        CGFloat viewW = self.frame.size.width;
        CGFloat viewH = self.frame.size.height;
        UIImageView *topView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, viewW, 30)];
        topView.userInteractionEnabled = true;
        [topView setImage:[UIImage imageNamed:@"firstImage"].resizbleImage];
        [self.contentView addSubview:topView];
        // 上面删除按钮
        UIButton *delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [delBtn setImage:[UIImage imageWithOriginalName:@"cancelCollectionImage"].resizbleImage forState:UIControlStateNormal];
        [delBtn setImage:[UIImage imageWithOriginalName:@"cancelCollectionImage"].resizbleImage forState:UIControlStateHighlighted];
        [delBtn setImage:[UIImage imageWithOriginalName:@"selectedCancelCollectionImage"].resizbleImage forState:UIControlStateSelected];
        delBtn.selected = false;
        delBtn.frame = CGRectMake(viewW - 55, 0, 25, 30);
        [delBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:delBtn];
        
        // http://www.itstrike.cn/question/94e1c8c1-daae-4b7d-a34b-36fd7bb7ed99.html
        
        
        UITableView *collectionWordTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, viewW, viewH - 30) style:UITableViewStyleGrouped];
        collectionWordTableView.backgroundColor = [UIColor colorFromHexRGB:@"f2f2f2"];
        //wrongWordTableView.backgroundColor = [UIColor redColor];
        collectionWordTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        collectionWordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //wrongWordTableView.backgroundColor = [UIColor colorFromHexRGB:@"f2f2f2"];
        [collectionWordTableView registerNib:[UINib nibWithNibName:NSStringFromClass([WordBookNormalCell class]) bundle:nil] forCellReuseIdentifier:collectionWordCellID];
        collectionWordTableView.delegate = self;
        collectionWordTableView.dataSource = self;
        _collectionWordTableView = collectionWordTableView;
        [self.contentView addSubview:collectionWordTableView];
        
        
        // 全选 === 删除
        UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, viewH, kScreenW, 75)];
        bottomView.layer.shadowColor = [[UIColor lightGrayColor] CGColor];
        bottomView.layer.shadowOffset = CGSizeMake(0, -3);
        bottomView.layer.shadowOpacity = 0.5;
        bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView = bottomView;
        [self.contentView addSubview:bottomView];
        
        
        //全选
        UIButton *selecAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [selecAllBtn setImage:[UIImage imageWithOriginalName:@"unselectedAll"].resizbleImage forState:UIControlStateNormal];
        [selecAllBtn setImage:[UIImage imageWithOriginalName:@"unselectedAll"].resizbleImage forState:UIControlStateHighlighted];
        [selecAllBtn setImage:[UIImage imageWithOriginalName:@"selectedAll"].resizbleImage forState:UIControlStateSelected];
        [selecAllBtn addTarget:self action:@selector(selecAllBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [selecAllBtn setTitle:@"   全选" forState:UIControlStateNormal];
        selecAllBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [selecAllBtn setTitleColor:[UIColor colorFromHexRGB:@"333333"] forState:UIControlStateNormal];
        selecAllBtn.frame = CGRectMake(0, 0, viewW * 0.5, 75);
        _selectedAllBtn = selecAllBtn;
        [bottomView addSubview:selecAllBtn];
        // 删除
        UIButton *sureDeldteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [sureDeldteBtn addTarget:self action:@selector(sureDeleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [sureDeldteBtn setTitle:@"删除" forState:UIControlStateNormal];
        sureDeldteBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [sureDeldteBtn setTitleColor:[UIColor colorFromHexRGB:@"333333"] forState:UIControlStateNormal];
        sureDeldteBtn.frame = CGRectMake(viewW * 0.5, 0, viewW * 0.5, 75);
        [bottomView addSubview:sureDeldteBtn];
        
        
    }
    
    
    
    
    //#pragma mark - 去复习
    //- (void)initWithTarget:(id)target action:(SEL)action{
    //    [self.reviewBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    //}
    
    #pragma mark - UITableView 代理方法
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.collectionWordArr.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        NSDictionary *dic = self.collectionWordArr[section];
        NSArray *array = dic[DicValue];
        return array.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSDictionary *dic = self.collectionWordArr[indexPath.section];
        NSArray *array = dic[DicValue];
        //NSLog(@"section:%zd,row:%zd",indexPath.section,indexPath.row);
        if (self.deleteFlag) { // 删除状态
            WordBookNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:collectionWordCellID forIndexPath:indexPath];
            CollectionWordModel *model = array[indexPath.row];
            cell.collectionModel = model;
            // 状态 normal(正常状态) selected(选中状态) unselected(非选中状态)
           // NSLog(@"model.stateStr---%@",model.stateStr);
            if ([model.stateStr isEqualToString:@"selected"]) {
                if (indexPath.row == array.count - 1) {
                    [cell initWithWrongWordModel:nil imageName:@"thirdSelectImage"];
                }
                
            }else if ([model.stateStr isEqualToString:@"unselected"]){
                if (indexPath.row == array.count - 1) {
                    [cell initWithWrongWordModel:nil imageName:@"thirdunSelectImage"];
                }
            }
            return cell;
        }else{ // 正常状态
            WordBookNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:collectionWordCellID forIndexPath:indexPath];
            NSDictionary *dic = self.collectionWordArr[indexPath.section];
            NSArray *array = dic[DicValue];
            CollectionWordModel *model = array[indexPath.row];
            cell.collectionModel = model;
            if (indexPath.row == array.count - 1) { // 最后一个
                [cell initWithWrongWordModel:nil imageName:@"fourImage"];
            }
            return cell;
        }
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"点击cell -- %zd",indexPath.row);
        NSDictionary *dic = self.collectionWordArr[indexPath.section];
        NSArray *array = dic[DicValue];
        CollectionWordModel *model = array[indexPath.row];
        //NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
        if ([model.stateStr isEqualToString:@"selected"]) {
            model.stateStr = @"unselected";
            if (((NSNumber *)dic[SectionSelect]).integerValue == 1) {
                NSDictionary *tempDic = @{DicKey:dic[DicKey],DicValue:dic[DicValue],SectionSelect:@(0)};
                [self.collectionWordArr replaceObjectAtIndex:indexPath.section withObject:tempDic];
            }
            if (self.selectedAllBtn.selected) {
                self.selectedAllBtn.selected = false;
            }
        }else if ([model.stateStr isEqualToString:@"unselected"]){
            model.stateStr = @"selected";
            NSInteger selectRowNum = 0;
            for (CollectionWordModel *model in array) {
                if ([model.stateStr isEqualToString:@"selected"]) {
                    selectRowNum ++;
                }
            }
            
            if (selectRowNum == array.count) { // 这组已经全选了
                NSDictionary *tempDic = @{DicKey:dic[DicKey],DicValue:dic[DicValue],SectionSelect:@(1)};
                [self.collectionWordArr replaceObjectAtIndex:indexPath.section withObject:tempDic];
            }else{
                 NSDictionary *tempDic = @{DicKey:dic[DicKey],DicValue:dic[DicValue],SectionSelect:@(0)};
                 [self.collectionWordArr replaceObjectAtIndex:indexPath.section withObject:tempDic];
            }
            // 选中头的个数   如果选中头的个数 == section的个数  ----> 全选按钮选中
            NSInteger sectionSelectedNum = 0;
            for (NSDictionary *tempDic in self.collectionWordArr) {
                if (((NSNumber *)tempDic[SectionSelect]).integerValue == 1) {
                    sectionSelectedNum ++;
                }
            }
            if (sectionSelectedNum == self.collectionWordArr.count) {
                self.selectedAllBtn.selected = true;
            }
            
            
        }else{ // 跳转查看详情
            NSLog(@"跳转详情");
        }
        
        [tableView reloadData];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 44.0f;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 25.0f;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 20.0f;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        NSDictionary *dic = self.collectionWordArr[section];
        // sectionHeaderViewID
        WrongWordHeaderSectionView *sectionView = (WrongWordHeaderSectionView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:sectionHeaderView];
        if (!sectionView) {
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderClickAction:)];
            sectionView = [[WrongWordHeaderSectionView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 25.0f)];
            sectionView.tag = section;
            [sectionView addGestureRecognizer:tapGes];
            [sectionView initWithDateStr:dic[DicKey]];
        }
        if (self.deleteFlag) { // 删除状态
            NSNumber *sectionSelect = dic[SectionSelect];
            if (sectionSelect.integerValue == 0) {// 没选中
                [sectionView initWithImageName:@"firstunSelectImage"];
            }else{
                [sectionView initWithImageName:@"firstSelectImage"];
            }
        }else{
            [sectionView initWithImageName:@"secondImage"];
        }
        return sectionView;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
        WrongWordFooterSectionView *footerView = [[WrongWordFooterSectionView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 25.0f)];
        return footerView;
    }
    
    #pragma mark - 上面删除按钮
    - (void)deleteBtnAction:(UIButton *)cancelBtn{
        cancelBtn.selected = !cancelBtn.selected;
        
        CGFloat viewH = self.frame.size.height;
        if(cancelBtn.selected){ // 选中状态
            self.deleteFlag = true;// 修改状态为 删除状态
            // 状态 normal(正常状态) selected(选中状态) unselected(非选中状态)
            for (NSDictionary *dic in self.collectionWordArr) {
                NSArray *array = dic[DicValue];
                for (CollectionWordModel *model in array) {
                    model.stateStr = @"unselected";
                }
            }
            
            [self.collectionWordTableView reloadData];
            for (int i = 0; i < self.collectionWordArr.count; i ++) {
                WrongWordHeaderSectionView * headView = (WrongWordHeaderSectionView *)[self.collectionWordTableView headerViewForSection:i];
                [headView initWithImageName:@"firstunSelectImage"];
            }
            [UIView animateWithDuration:0.25 animations:^{
                self.bottomView.mj_y = viewH - 75;
                self.collectionWordTableView.mj_h = viewH - 30 - 75;
            }];
        }else{ // 非选中状态
            self.deleteFlag = false;// 修改状态为 删除状态
            self.selectedAllBtn.selected = false;
            for (int i = 0; i < self.collectionWordArr.count; i ++) {
                NSDictionary *dic = self.collectionWordArr[i];
                for (CollectionWordModel *model in dic[DicValue]) {
                    model.stateStr = @"normal";
                }
                NSDictionary *tempDic = @{DicKey:dic[DicKey],DicValue:dic[DicValue],SectionSelect:@(0)};
                [self.collectionWordArr replaceObjectAtIndex:i withObject:tempDic];
            }
            [self.collectionWordTableView reloadData];
            
            [UIView animateWithDuration:0.25 animations:^{
                self.bottomView.mj_y = viewH;
                self.collectionWordTableView.mj_h = viewH - 30;
            }];
        }
    }
    
    #pragma mark - sectionHeader点击
    - (void)sectionHeaderClickAction:(UITapGestureRecognizer *)tapGes{
        //NSLog(@"sectionHeader点击");
        WrongWordHeaderSectionView *sectionView = (WrongWordHeaderSectionView *)tapGes.view;
        NSInteger sectionTag = sectionView.tag;
        NSMutableDictionary *sectionDic = self.collectionWordArr[sectionTag];
        
        //NSLog(@"sectionDic----%@",sectionDic);
        if (!self.deleteFlag) { // 不是删除状态
            [sectionView initWithImageName:@"secondImage"];
        }else{ // 是删除状态 1.如果是选中状态 点击是不选中状态  2.如果是不选中状态 点击是选中状态
            if ([sectionView.backImageView.image isEqual:[UIImage imageWithOriginalName:@"firstunSelectImage"].resizbleImage]) { // 非选中状态
                //NSLog(@"不选中状态:%@",sectionDic[SectionSelect]);
                [sectionView initWithImageName:@"firstSelectImage"];
                // 状态 normal(正常状态) selected(选中状态) unselected(非选中状态)
                for (CollectionWordModel *model in sectionDic[DicValue]) {
                    model.stateStr = @"selected";
                }
                NSDictionary *dic = @{DicKey:sectionDic[DicKey],DicValue:sectionDic[DicValue],SectionSelect:@(1)};
                [self.collectionWordArr replaceObjectAtIndex:sectionTag withObject:dic];
            }else if ([sectionView.backImageView.image isEqual:[UIImage imageWithOriginalName:@"firstSelectImage"].resizbleImage]){ // 选中状态
                //NSLog(@"选中状态");
                [sectionView initWithImageName:@"firstunSelectImage"];
                // 状态 normal(正常状态) selected(选中状态) unselected(非选中状态)
                for (CollectionWordModel *model in sectionDic[DicValue]) {
                    model.stateStr = @"unselected";
                }
                NSDictionary *dic = @{DicKey:sectionDic[DicKey],DicValue:sectionDic[DicValue],SectionSelect:@(0)};
                [self.collectionWordArr replaceObjectAtIndex:sectionTag withObject:dic];
            }
        }
        
        // 选中头的个数   如果选中头的个数 == section的个数  ----> 全选按钮选中
        NSInteger sectionSelectedNum = 0;
        for (NSDictionary *tempDic in self.collectionWordArr) {
            if (((NSNumber *)tempDic[SectionSelect]).integerValue == 1) {
                sectionSelectedNum ++;
            }
        }
        if (sectionSelectedNum == self.collectionWordArr.count) {
            self.selectedAllBtn.selected = true;
        }else{
            self.selectedAllBtn.selected = false;
        }
        
        
        [self.collectionWordTableView reloadData];
    }
    
    #pragma mark - 全选
    - (void)selecAllBtnAction:(UIButton *)selectAllBtn{
        selectAllBtn.selected = !selectAllBtn.selected;
        if (selectAllBtn.selected) { // 选中状态
            
            
            for (int i = 0; i < self.collectionWordArr.count; i ++) {
                NSDictionary *dic = self.collectionWordArr[i];
                for (CollectionWordModel *model in dic[DicValue]) {
                    model.stateStr = @"selected";
                }
                NSDictionary *tempDic = @{DicKey:dic[DicKey],DicValue:dic[DicValue],SectionSelect:@(1)};
                [self.collectionWordArr replaceObjectAtIndex:i withObject:tempDic];
            }
            [self.collectionWordTableView reloadData];
        }else{ // 非选中状态
            
            // 状态 normal(正常状态) selected(选中状态) unselected(非选中状态)
            for (int i = 0; i < self.collectionWordArr.count; i ++) {
                NSDictionary *dic = self.collectionWordArr[i];
                for (CollectionWordModel *model in dic[DicValue]) {
                    model.stateStr = @"unselected";
                }
                NSDictionary *tempDic = @{DicKey:dic[DicKey],DicValue:dic[DicValue],SectionSelect:@(0)};
                [self.collectionWordArr replaceObjectAtIndex:i withObject:tempDic];
            }
            [self.collectionWordTableView reloadData];
        }
    }
    
    #pragma mark - 下面删除
    - (void)sureDeleteBtnAction{
        [self.wantDeleteDataArr removeAllObjects];
        for (NSDictionary *dic in self.collectionWordArr) {
            for (CollectionWordModel *model in dic[DicValue]) {
                if ([model.stateStr isEqualToString:@"selected"]) {
                    [self.wantDeleteDataArr addObject:model.wordId];
                }
            }
        }
        if (self.wantDeleteDataArr.count > 0) {
            self.alertView.hidden = false;
        }else{
            [commonTools showHudTitle:@"请选中要删除的数据" inView: self.contentView];
        }
    }
    
    
    #pragma mark - 提示框 取消删除
    - (void)alertCancelAction{
        self.alertView.hidden = true;
        NSLog(@"取消删除");
    }
    
    #pragma mark - 提示框 确定删除
    - (void)alertOKAction{
        NSLog(@"确定删除");
    }
    
    #pragma mark - 按日期分组
    - (NSArray *)sortArr:(NSArray *)dataArray{
        // 首先把原数组中数据的日期取出来放入dateStrArr
        NSMutableArray *dateStrArr = [NSMutableArray array];
        [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CollectionWordModel *model = obj;
            [dateStrArr addObject:model.createDate];
        }];
        // 使用asset把timeArr的日期去重
        NSSet *set = [NSSet setWithArray:dateStrArr];
        NSArray *userArray = [set allObjects];
        NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];//yes升序排列,no,降序排列
        //按日期降序排列的日期数组
        NSArray *sortDateArr = [userArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];
        //此时得到的sortDateArr就是按照时间降序排列拍好的数组
        NSMutableArray *sortDataArray = [NSMutableArray array];
        [sortDateArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSMutableArray *arr = [NSMutableArray array];
            NSDictionary *dic = @{DicKey:(NSString *)obj,DicValue:arr,SectionSelect:@(0)};
            [sortDataArray addObject:dic]; // 每一个日期都是一个字典
        }];
        
        
        
        //遍历_dataArray取其中每个数据的日期看看与myary里的那个日期匹配就把这个数据装到_titleArray 对应的组中
        [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CollectionWordModel *model = obj;
            for (NSString *eachDateStr in sortDateArr) {
                if ([model.createDate isEqualToString: eachDateStr]) {
                    //NSLog(@"%zd",[dateStrArr indexOfObject:eachDateStr]);
                    NSDictionary *dic = [sortDataArray objectAtIndex:[sortDateArr indexOfObject:eachDateStr]];
                    NSMutableArray *array = dic[DicValue];
                    [array addObject:model];
                }
            }
        }];
        return sortDataArray;
    }
    @end
    
    没有内容图示框

    .h文件

    #import <UIKit/UIKit.h>
    
    @interface NoContentTipView : UIView
    - (void)initWithImageName:(NSString *)imageName tipText:(NSString *)tipText;
    @end
    

    .m文件

    #import "NoContentTipView.h"
    
    @interface NoContentTipView()
    /**图片*/
    @property (nonatomic, strong) UIImageView *tipImageView;
    /**文字*/
    @property (nonatomic, strong) UILabel *tipLB;
    @end
    
    @implementation NoContentTipView
    
    - (instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = [UIColor colorFromHexRGB:@"f2f2f2"];
            [self setUpSubviews];
        }
        return self;
    }
    
    - (void)setUpSubviews{
        /**图片宽*/
        CGFloat imageW = 119;
        /**图片高*/
        CGFloat imageH = 80;
        /**View宽*/
        CGFloat viewW = self.bounds.size.width;
        /**View高*/
        CGFloat viewH = self.bounds.size.height;
        /**图片X*/
        CGFloat imageX = (viewW - imageW) * 0.5;
        /**图片Y*/
        CGFloat imageY = 115;
        UIImageView *tipImageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
        _tipImageView = tipImageView;
        [self addSubview:tipImageView];
        
        UILabel *tipLB = [[UILabel alloc]init];
        tipLB.textColor = [UIColor colorFromHexRGB:@"999999"];
        tipLB.font = [UIFont systemFontOfSize:15];
        tipLB.textAlignment = NSTextAlignmentCenter;
        _tipLB = tipLB;
        [self addSubview:tipLB];
        [tipLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX);
            make.top.mas_equalTo(tipImageView.mas_bottom).offset(21);
            make.width.mas_greaterThanOrEqualTo(10);
            make.height.mas_equalTo(16);
        }];
    }
    
    
    - (void)initWithImageName:(NSString *)imageName tipText:(NSString *)tipText{
        self.tipImageView.image = [UIImage imageWithOriginalName:imageName].resizbleImage;
        self.tipLB.text = tipText;
    }
    @end
    

    相关文章

      网友评论

        本文标题:后台返回一组数据(一个数组), 然后按照日期进行分组, 用UIT

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