效果圖以下:緩存
1.找到 SDWebImage找到SDImageCache類atom
2.添加以下方法lua
- (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:_diskCachePath]; for (NSString *fileName in fileEnumerator) { NSString *filePath = [_diskCachePath stringByAppendingPathComponent:fileName]; NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; unsigned long long length = [attrs fileSize]; totalSize += length / 1024.0 / 1024.0; } // NSLog(@"tmp size is %.2f",totalSize); return totalSize; }
具體用tableView實現spa
MoreViewController.h代理
#import "BaseViewController.h" #import "More.h" @interface MoreViewController : BaseViewController<UITableViewDataSource,UITableViewDelegate> @property(strong,nonatomic)UITableView *MoreTableView; @property(strong,nonatomic)NSArray *arrayTitle; @property(strong,nonatomic)NSArray *arrayImage; @end
MoreViewController.m code
#import "MoreViewController.h" @interface MoreViewController () @property(strong,nonatomic)UILabel *lbl; @end @implementation MoreViewController - (void)viewDidLoad { [super viewDidLoad]; self.title=@"更多"; [self _loadData]; NSLog(@"%@",NSHomeDirectory()); } -(void)_loadData { // 設置tableview的title和image self.arrayTitle=@[@"清除緩存",@"給個評價",@"商務合做",@"檢測行版本",@"歡迎頁",@"關於"]; self.arrayImage=@[@"moreClear@2x",@"moreScore@2x",@"moreBusiness@2x",@"moreVersion@2x",@"moreWelcome@2x",@"moreAbout@2x"]; // 初始化UItableView self.MoreTableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; // 設置UItableView的背景色 self.MoreTableView.backgroundColor=[UIColor blackColor]; // 指定代理 self.MoreTableView.delegate=self; self.MoreTableView.dataSource=self; // 分割線的樣式 self.MoreTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; [self.view addSubview:self.MoreTableView]; // 指定惟一標識符 [self.MoreTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MoreTableCell"]; self.lbl=[[UILabel alloc]init]; self.lbl.textColor=[UIColor whiteColor]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arrayTitle.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreTableCell" forIndexPath:indexPath]; if (indexPath.row==0) { self.lbl.frame= CGRectMake(200,20,200, cell.frame.size.height*0.5); self.lbl.text=[NSString stringWithFormat:@"緩存爲%.2fMB",[[SDImageCache sharedImageCache] checkTmpSize]]; [cell addSubview:self.lbl]; } // 設置Cell的背景色 cell.backgroundColor=[UIColor blackColor]; // 設置title的文字顏色 cell.textLabel.textColor=[UIColor whiteColor]; cell.textLabel.text=self.arrayTitle[indexPath.row]; cell.imageView.image=[UIImage imageNamed:self.arrayImage[indexPath.row]]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } #pragma Mark - 選中跳轉 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row==0) { [self alertView]; }else if (indexPath.row==1){ EvaluationViewController *evaluation=[EvaluationViewController new]; [self.navigationController pushViewController:evaluation animated:YES]; } else if (indexPath.row==2){ BusinessViewController *business=[BusinessViewController new]; [self.navigationController pushViewController:business animated:YES]; }else if (indexPath.row==3){ VersionViewController *version=[VersionViewController new]; [self.navigationController pushViewController:version animated:YES]; }else if (indexPath.row==4){ WelcomeViewController *welcome=[WelcomeViewController new]; [self.navigationController pushViewController:welcome animated:YES]; }else{ AboutViewController *about=[AboutViewController new]; [self.navigationController pushViewController:about animated:YES]; } } #pragma Mark - 彈出框 -(void)alertView { NSLog(@"-----"); float tmpSize=[[SDImageCache sharedImageCache] checkTmpSize]; UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"清理緩存(%.2fM)",tmpSize] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok=[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 清除磁盤 [[SDImageCache sharedImageCache] clearDisk]; // 刷新數據 [self.MoreTableView reloadData]; }]; UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:ok]; [alert addAction:cancle]; [self presentViewController:alert animated:YES completion:nil]; } #pragma Mark - 總體刷新 -(void)viewWillAppear:(BOOL)animated { // [super viewWillAppear:YES]; [self.MoreTableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end