清空缓存
2015-11-17 11:33
141 查看
1.添加一个属性
@property (nonatomic, retain) UILabel *contentLabel;
2.懒加载
- (UILabel *)contentLabel {
if (!_contentLabel) {
self.contentLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)] autorelease];
self.contentLabel.textColor = [UIColor lightGrayColor];
self.contentLabel.adjustsFontSizeToFitWidth = YES;
}
return [[_contentLabel retain] autorelease];
}
3.cellForRow方法中
//清除缓存
if (indexPath.section == 0 && indexPath.row == 1) {
self.contentLabel.text = [self cleanUnussFile];
cell.detailTextLabel.text = self.contentLabel.text;
}
- (NSString *)getCachesPath {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
return path;
}
- (CGFloat)folderSizeAtPath:(NSString *)floderPath {
NSFileManager *manger = [NSFileManager defaultManager];
floderPath = [self getCachesPath];
if (![manger fileExistsAtPath:floderPath]) {
return 0;
}
NSEnumerator *childFilesEnumerator = [[manger subpathsAtPath:floderPath] objectEnumerator];
NSString *fileName = nil;
long long floderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil) {
NSString *fileAbsilutePath = [floderPath stringByAppendingPathComponent:fileName];
float singleFileSize = 0.0;
if ([manger fileExistsAtPath:fileAbsilutePath]) {
singleFileSize = [[manger attributesOfItemAtPath:fileAbsilutePath error:nil] fileSize];
}
floderSize += singleFileSize;
}
return floderSize;
}
(NSString *)cleanUnussFile {
[super viewWillAppear:YES];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = [self getCachesPath];
CGFloat allSize = 0;
for (NSString *fileName in [manager subpathsAtPath:path]) {
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSDictionary *attribute = [manager attributesOfItemAtPath:filePath error:nil];
//计算缓存文件大小
double size = [attribute[NSFileSize] floatValue];
size = size / 1024.0 / 1024.0;
allSize += size;
}
NSString *clearnStr = [NSString stringWithFormat:@”%0.0lfMB”, allSize];
self.tabBarController.tabBar.height = NO;
return clearnStr;
}
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
return;
} else {
NSString *cachePath = [self getCachesPath];
NSFileManager *manager = [NSFileManager defaultManager];
for (NSString *fileName in [manager subpathsAtPath:cachePath]) {
NSString *filePath = [cachePath stringByAppendingPathComponent:fileName];
[manager removeItemAtPath:filePath error:nil];
self.contentLabel.text = @”0.00MB”;
}
[self.tableview reloadData];
}
}
@property (nonatomic, retain) UILabel *contentLabel;
2.懒加载
- (UILabel *)contentLabel {
if (!_contentLabel) {
self.contentLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)] autorelease];
self.contentLabel.textColor = [UIColor lightGrayColor];
self.contentLabel.adjustsFontSizeToFitWidth = YES;
}
return [[_contentLabel retain] autorelease];
}
3.cellForRow方法中
//清除缓存
if (indexPath.section == 0 && indexPath.row == 1) {
self.contentLabel.text = [self cleanUnussFile];
cell.detailTextLabel.text = self.contentLabel.text;
}
pragma mark—-清空缓存
//获取缓存路径- (NSString *)getCachesPath {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
return path;
}
- (CGFloat)folderSizeAtPath:(NSString *)floderPath {
NSFileManager *manger = [NSFileManager defaultManager];
floderPath = [self getCachesPath];
if (![manger fileExistsAtPath:floderPath]) {
return 0;
}
NSEnumerator *childFilesEnumerator = [[manger subpathsAtPath:floderPath] objectEnumerator];
NSString *fileName = nil;
long long floderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil) {
NSString *fileAbsilutePath = [floderPath stringByAppendingPathComponent:fileName];
float singleFileSize = 0.0;
if ([manger fileExistsAtPath:fileAbsilutePath]) {
singleFileSize = [[manger attributesOfItemAtPath:fileAbsilutePath error:nil] fileSize];
}
floderSize += singleFileSize;
}
return floderSize;
}
(NSString *)cleanUnussFile {
[super viewWillAppear:YES];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = [self getCachesPath];
CGFloat allSize = 0;
for (NSString *fileName in [manager subpathsAtPath:path]) {
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSDictionary *attribute = [manager attributesOfItemAtPath:filePath error:nil];
//计算缓存文件大小
double size = [attribute[NSFileSize] floatValue];
size = size / 1024.0 / 1024.0;
allSize += size;
}
NSString *clearnStr = [NSString stringWithFormat:@”%0.0lfMB”, allSize];
self.tabBarController.tabBar.height = NO;
return clearnStr;
}
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
return;
} else {
NSString *cachePath = [self getCachesPath];
NSFileManager *manager = [NSFileManager defaultManager];
for (NSString *fileName in [manager subpathsAtPath:cachePath]) {
NSString *filePath = [cachePath stringByAppendingPathComponent:fileName];
[manager removeItemAtPath:filePath error:nil];
self.contentLabel.text = @”0.00MB”;
}
[self.tableview reloadData];
}
}
相关文章推荐
- execve函数族详解
- 如何重新签名ipa文件
- dnsyo - 在全球超过1500个DNS服务器上检查你的域名解析记录
- SAN存储网络
- ***微信中用户提交信息的流程(网址接入流程)
- linux下gcc/g++编译器使用简介及makefile的书写规则
- JQuery 学习,翻译练习
- Java中不规则二维数组运用
- LeetCode -- Sum Root to Leaf Numbers
- 解决HorizontalScrollView与SeekBar滑动冲突的问题.
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
- 有关animation抽屉动画示例
- python判断文件类型
- 程序员整理的各种不错的工具
- 利用OpenXml生成Word2007文档
- ActiveMQ 即时通讯服务 浅析
- SEO各名词基本概念
- 编译修改tomcat7源码,支持同时设置允许ip和域名
- 进程和线程的一些问题
- 高效求素数--筛法求素数