Reputation: 39
I have a tableView. If I click on the cell, it starts downloading file with UIActivityIndicator
animation. Once the download is complete checkmark appears (the file exists), and the user can move to the next controller. Necessary that after move to the next controller and return back all checkmark disappeared. How to do it?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"Cell%d", indexPath.row] forIndexPath:indexPath];
if (indexPath.row == 1){
if (!fileExists) {
[_spinner startAnimating];
}
if (fileExists) {
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (indexPath.row == 2){
if (!fileExists1) {
[_spinner1 startAnimating];
}
if (fileExists1) {
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1) {
if (!fileExists) {
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_spinner.frame = CGRectMake(0, 0, 24, 24);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = _spinner;
tableView cellForRowAtIndexPath:indexPath].accessoryView = _spinner;
[_spinner startAnimating];
if (fileExists) {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
}
Upvotes: 0
Views: 125
Reputation: 15
If fileExists, reload your tableView in viewWillAppear. Use a model to mark the fileExists cell.
@interface CheckMarkModel ()
@property (assign, nonatomic,get=isFileExists) BOOL fileExists;
@end
@implementation CheckMarkModel
@end
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSIndexPath selectIndexPath; // select IndexPath
@property (strong, nonatomic) NSMutableArray *dataArrM; // save select cell
@end
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// your cell count
NSInteger cellCount = ;
_dataArrM = [NSMutableArray array];
for (int i = 0; i < cellCount; ++i)
{
CheckMarkModel *model = [CheckMarkModel new];
[_dataArrM addObject:model];
}
// reload
[tableView reload];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
//....
CheckMarkModel *model = _dataArrM[indexPath.row];
cell.accessoryType = (model.isFileExists) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.isFileExists)
{
_selectIndexPath = indexPath;
[self downloadFile];
[self tableView:tableView didDeselectRowAtIndexPath:indexPath];
}else {
// move to the next controller
}
}
- (void)downloadFile {
// download action ...
//download success
if (fileExists) {
CheckMarkModel *model = _dataArrM[_selectIndexPath.row];
model.fileExists = YES;
[tableView reloadRowsAtIndexPaths:@[_selectIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
@end
Upvotes: 0
Reputation: 26924
Check the result:
Follow my code:
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *sel_indexPath; // selected indexpath
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// cancel the checkmark
if (self.sel_indexPath) {
[self.tableView reloadData];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellId"];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = @"cell title";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
self.sel_indexPath = indexPath;
[self performSegueWithIdentifier:@"VC1GotoVC2" sender:self];
}
@end
Upvotes: 0