您的位置:首页 > 其它

清空缓存

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;

}

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];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: