后臺返回一組數(shù)據(jù)(一個數(shù)組), 然后按照日期進(jìn)行分組, 用UITableView展示

數(shù)組按照日期分組.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;
/**狀態(tài) normal(正常狀態(tài)) selected(選中狀態(tài)) unselected(非選中狀態(tài))*/
@property (nonatomic, strong) NSString *stateStr; // 自己定義的,類似點贊
@end

.m文件

#import "CollectionWordModel.h"
@implementation CollectionWordModel
@end
分組方法
#pragma mark - 按日期分組
- (NSArray *)sortArr:(NSArray *)dataArray{
    // 首先把原數(shù)組中數(shù)據(jù)的日期取出來放入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,降序排列
    //按日期降序排列的日期數(shù)組
    NSArray *sortDateArr = [userArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];
    //此時得到的sortDateArr就是按照時間降序排列拍好的數(shù)組
    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取其中每個數(shù)據(jù)的日期看看與myary里的那個日期匹配就把這個數(shù)據(jù)裝到_titleArray 對應(yīng)的組中
    [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;
}

詳細(xì)代碼

//
//  CollectionWordItem.m
//  SparkWordNew
//
//  Created by 萬艷勇 on 2017/12/30.
//  Copyright ? 2017年 李凌飛. All rights reserved.
//

#import "CollectionWordItem.h"
#import "NoContentTipView.h" // 沒有內(nèi)容提示
#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;
/**收藏單詞數(shù)據(jù)*/
@property (nonatomic, strong) NSMutableArray *collectionWordArr;
/**用于提示*/
@property (nonatomic, strong) NoContentTipView *tipView;
/**去復(fù)習(xí)*/
@property (nonatomic, strong) UIButton *reviewBtn;
/**底部用于顯示 全選 刪除按鈕視圖*/
@property (nonatomic, strong)  UIView *bottomView;
/**全選按鈕*/
@property (nonatomic, strong) UIButton *selectedAllBtn;

/**AlertView*/
@property (nonatomic, strong) CustomAlertView *alertView;

/**保存要刪除的數(shù)組*/
@property (nonatomic, strong) NSMutableArray *wantDeleteDataArr;

/**是否是刪除狀態(tài) 默認(rèn)是false 正常狀態(tài) treu 是刪除狀態(tài)*/
@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; // 默認(rèn)是 正常狀態(tài)
        [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{ // 解析數(shù)據(jù) , 刷新列表
            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 - 去復(fù)習(xí)
//- (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) { // 刪除狀態(tài)
        WordBookNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:collectionWordCellID forIndexPath:indexPath];
        CollectionWordModel *model = array[indexPath.row];
        cell.collectionModel = model;
        // 狀態(tài) normal(正常狀態(tài)) selected(選中狀態(tài)) unselected(非選中狀態(tài))
       // 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{ // 正常狀態(tài)
        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) { // 這組已經(jīng)全選了
            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];
        }
        // 選中頭的個數(shù)   如果選中頭的個數(shù) == section的個數(shù)  ----> 全選按鈕選中
        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{ // 跳轉(zhuǎn)查看詳情
        NSLog(@"跳轉(zhuǎn)詳情");
    }
    
    [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) { // 刪除狀態(tài)
        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){ // 選中狀態(tài)
        self.deleteFlag = true;// 修改狀態(tài)為 刪除狀態(tài)
        // 狀態(tài) normal(正常狀態(tài)) selected(選中狀態(tài)) unselected(非選中狀態(tài))
        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{ // 非選中狀態(tài)
        self.deleteFlag = false;// 修改狀態(tài)為 刪除狀態(tài)
        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) { // 不是刪除狀態(tài)
        [sectionView initWithImageName:@"secondImage"];
    }else{ // 是刪除狀態(tài) 1.如果是選中狀態(tài) 點擊是不選中狀態(tài)  2.如果是不選中狀態(tài) 點擊是選中狀態(tài)
        if ([sectionView.backImageView.image isEqual:[UIImage imageWithOriginalName:@"firstunSelectImage"].resizbleImage]) { // 非選中狀態(tài)
            //NSLog(@"不選中狀態(tài):%@",sectionDic[SectionSelect]);
            [sectionView initWithImageName:@"firstSelectImage"];
            // 狀態(tài) normal(正常狀態(tài)) selected(選中狀態(tài)) unselected(非選中狀態(tài))
            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]){ // 選中狀態(tài)
            //NSLog(@"選中狀態(tài)");
            [sectionView initWithImageName:@"firstunSelectImage"];
            // 狀態(tài) normal(正常狀態(tài)) selected(選中狀態(tài)) unselected(非選中狀態(tài))
            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];
        }
    }
    
    // 選中頭的個數(shù)   如果選中頭的個數(shù) == section的個數(shù)  ----> 全選按鈕選中
    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) { // 選中狀態(tài)
        
        
        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{ // 非選中狀態(tài)
        
        // 狀態(tài) normal(正常狀態(tài)) selected(選中狀態(tài)) unselected(非選中狀態(tài))
        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:@"請選中要刪除的數(shù)據(jù)" inView: self.contentView];
    }
}


#pragma mark - 提示框 取消刪除
- (void)alertCancelAction{
    self.alertView.hidden = true;
    NSLog(@"取消刪除");
}

#pragma mark - 提示框 確定刪除
- (void)alertOKAction{
    NSLog(@"確定刪除");
}

#pragma mark - 按日期分組
- (NSArray *)sortArr:(NSArray *)dataArray{
    // 首先把原數(shù)組中數(shù)據(jù)的日期取出來放入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,降序排列
    //按日期降序排列的日期數(shù)組
    NSArray *sortDateArr = [userArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];
    //此時得到的sortDateArr就是按照時間降序排列拍好的數(shù)組
    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取其中每個數(shù)據(jù)的日期看看與myary里的那個日期匹配就把這個數(shù)據(jù)裝到_titleArray 對應(yīng)的組中
    [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
沒有內(nèi)容圖示框

.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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末把曼,一起剝皮案震驚了整個濱河市注盈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌叙赚,老刑警劉巖老客,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異震叮,居然都是意外死亡胧砰,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門苇瓣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尉间,“玉大人,你說我怎么就攤上這事击罪≌艹埃” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵媳禁,是天一觀的道長眠副。 經(jīng)常有香客問我,道長竣稽,這世上最難降的妖魔是什么囱怕? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮毫别,結(jié)果婚禮上娃弓,老公的妹妹穿的比我還像新娘。我一直安慰自己拧烦,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布钝计。 她就那樣靜靜地躺著恋博,像睡著了一般。 火紅的嫁衣襯著肌膚如雪私恬。 梳的紋絲不亂的頭發(fā)上债沮,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天,我揣著相機(jī)與錄音本鸣,去河邊找鬼疫衩。 笑死,一個胖子當(dāng)著我的面吹牛荣德,可吹牛的內(nèi)容都是我干的闷煤。 我是一名探鬼主播童芹,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鲤拿!你這毒婦竟也來了假褪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤近顷,失蹤者是張志新(化名)和其女友劉穎生音,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體窒升,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡缀遍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了饱须。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片域醇。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖冤寿,靈堂內(nèi)的尸體忽然破棺而出歹苦,到底是詐尸還是另有隱情,我是刑警寧澤督怜,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布殴瘦,位于F島的核電站厦凤,受9級特大地震影響锄禽,放射性物質(zhì)發(fā)生泄漏慕趴。R本人自食惡果不足惜铆惑,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一亡问、第九天 我趴在偏房一處隱蔽的房頂上張望闷沥。 院中可真熱鬧父虑,春花似錦单山、人聲如沸眼溶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽堂飞。三九已至灌旧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绰筛,已是汗流浹背枢泰。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留铝噩,地道東北人衡蚂。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親毛甲。 傳聞我的和親對象是個殘疾皇子年叮,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,838評論 25 707
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子丽啡,類似...
    liudhkk閱讀 9,012評論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫谋右、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,066評論 4 62
  • 上周末拖家?guī)Э诘厝チ颂税拈T补箍。 小屁孩對于出游已經(jīng)駕輕就熟了改执,沒有任何的不適應(yīng),一歲七個月的你出過兩次省坑雅,省內(nèi)游不下...
    初升的太陽曈曈閱讀 482評論 0 1
  • 今天身體不舒服辈挂,在床上躺了一天。正好花了一天的時間看完了英國作家克萊兒·麥克福爾的《擺渡人》裹粤。 這本書暢銷歐美33...
    風(fēng)中的糯米閱讀 594評論 2 1