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

数组按照日期分组.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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,905评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,140评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,791评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,483评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,476评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,516评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,905评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,560评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,778评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,557评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,635评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,338评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,925评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,898评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,142评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,818评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,347评论 2 342

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,376评论 25 707
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 8,978评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,016评论 4 62
  • 上周末拖家带口地去了趟澳门。 小屁孩对于出游已经驾轻就熟了,没有任何的不适应,一岁七个月的你出过两次省,省内游不下...
    初升的太阳曈曈阅读 476评论 0 1
  • 今天身体不舒服,在床上躺了一天。正好花了一天的时间看完了英国作家克莱儿·麦克福尔的《摆渡人》。 这本书畅销欧美33...
    风中的糯米阅读 584评论 2 1