[Objective-C] 纯文本查看 复制代码
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 设置加载视图
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[loadingView startAnimating];
self.accessoryView = loadingView;
//设置文字
self.textLabel.text = @"清楚缓存";
self.detailTextLabel.text = @"正在计算";
}
return self;
}
[Objective-C] 纯文本查看 复制代码
unsigned long long size =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"CustomFile"].fileSize;
//fileSize是封装在Category中的。
size += [SDImageCache sharedImageCache].getSize; //CustomFile + SDWebImage 缓存
//设置文件大小格式
NSString sizeText = nil;
if (size >= pow(10, 9)) {
sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
}else if (size >= pow(10, 6)) {
sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
}else if (size >= pow(10, 3)) {
sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
}else {
sizeText = [NSString stringWithFormat:@"%zdB", size];
}
[Objective-C] 纯文本查看 复制代码
//计算完成后,回到主线程继续处理,显示文件大小,除去加载视图,显示箭头,添加点击事件
dispatch_async(dispatch_get_main_queue(), ^{
self.detailTextLabel.text = [NSString stringWithFormat:@"%@",sizeText];
self.accessoryView = nil;
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCacheClick)]];
});
[Objective-C] 纯文本查看 复制代码
- (void)clearCacheClick
{
[SVProgressHUD showWithStatus:@"正在清除缓存···"];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr removeItemAtPath:GYLCustomFile error:nil];
[mgr createDirectoryAtPath:GYLCustomFile withIntermediateDirectories:YES attributes:nil error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
// 设置文字
self.detailTextLabel.text = nil;
});
});
}];
}
[Objective-C] 纯文本查看 复制代码
#import "GYLSettingViewController.h"
#import "GYLClearCacheCell.h"
@implementation GYLSettingViewController
static NSString * const GYLClearCacheCellID = @"ClearCache";
static NSString * const GYLSettingCellID = @"Setting";
- (instancetype)init
{
return [self initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = GYLBGColor;
self.navigationItem.title = @"设置";
[self.tableView registerClass:[GYLClearCacheCell class] forCellReuseIdentifier:GYLClearCacheCellID];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:GYLSettingCellID];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) {
return [[GYLClearCacheCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:GYLClearCacheCellID];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GYLSettingCellID];
cell.textLabel.text = [NSString stringWithFormat:@"section-%zd,row--%zd",indexPath.section,indexPath.row];
return cell;
}
@end