1.點(diǎn)擊單元格彈出提示
NSString *str = [self countCacheSize];
NSString *casheStr = [NSString stringWithFormat:@"緩存大小為%@,確認(rèn)清理嗎?",str];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"清理緩存" message:casheStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alertView show];
countCacheSize
- (NSString *)countCacheSize{
long long size = 0;
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSFileManager *fileMamager = [NSFileManager defaultManager];
//獲取文件夾下的子路徑
NSArray *pathArr = [fileMamager subpathsAtPath:path];
//拼接路徑
for (NSString *subPath in pathArr) {
NSString *allPath = [path stringByAppendingPathComponent:subPath];
//計(jì)算文件的大小
NSDictionary *fileDic = [fileMamager attributesOfItemAtPath:allPath error:nil];
size += [fileDic fileSize];
}
NSString *cache = [NSString stringWithFormat:@"%.2fM",size/1000.0/1000.0];
return cache;
}
2.代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex ==1){
NSString *filepath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSFileManager *fileManger = [NSFileManager defaultManager];
NSArray *arr = [fileManger subpathsAtPath:filepath];
for (NSString *subPath in arr) {
NSString *pathStr = [filepath stringByAppendingPathComponent:subPath];
[fileManger removeItemAtPath:pathStr error:nil];
//重新創(chuàng)建cache文件夾
[fileManger createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:nil error:nil];
[_tableView reloadRowsAtIndexPaths: @[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
}