/**字典的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