清空缓存

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];
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值