转载:http://www.tuicool.com/articles/nuaERjQ
#pragma mark - 计算缓存大小
2 - (NSString *)getCacheSize
3 {
4
5 long long sumSize = 0;
6
7
8 NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
9
10
11 NSFileManager *filemanager = [NSFileManager defaultManager];
12
13
14 NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
15
16 for (NSString *subPath in subPaths) {
17
18 NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
19
20 long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize];
21
22 sumSize += fileSize;
23 }
24 float size_m = sumSize/(1000*1000);
25 return [NSString stringWithFormat:@"%.2fM",size_m];
26
27 }
28 #pragma mark - 清除缓存提示(UITableViewDataSourceDelegate)
29 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
30 {
31 if (indexPath.row == 0) {
32 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"缓存清除" message:@"确定清除缓存?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
33 [alertView show];
34 }
35 }
36 #pragma mark - UIAlertViewDelegate方法实现
37 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
38 {
39 NSLog(@"代码执行到此");
40
41 if (buttonIndex == 1) {
42
43 NSFileManager *fileManager = [NSFileManager defaultManager];
44
45 NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
46
47 [fileManager removeItemAtPath:cacheFilePath error:nil];
48
49
50 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
51 [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
52
53
54 [_tableView reloadData];
55 }
56 @pragma -mark -放置于.m文件首段较为合适,本DEMO仅做功能性展示,实时监测缓存大小,从其他界面跳转到本页面,也需要刷新下表视图
57 - (void)viewWillAppear:(BOOL)animated
58 {
59 [super viewWillAppear:YES];
60 [_tableView reloadData];
61 }