2016-12-04 79 views
0

我有一个tableView。如果我点击单元格,它会开始用UIActivityIndicator动画下载文件。下载完成后,出现勾号(文件存在),用户可以移动到下一个控制器。必要的是,移动到下一个控制器并返回所有复选标记消失。怎么做?删除选中标记

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

} 
} 
} 
} 
+0

能否请你添加更多的代码,比如'cellForRowAtindexPath'和'didSelectRowAtIndexPath'。 –

+0

@用户检查答案,这是您的要求吗? – aircraft

+0

@NiravD我更新我的问题。请检查。 – user

回答

1

刷新你的tableview在viewWillAppear中

+0

' - (void)viewWillAppear:(BOOL)动画{self.tableView reloadData]; }'我添加代码,但它不工作 – user

+0

再次检查您的tableview对象,连接给定与否。 – Himanth

+0

我更新我的问题。请检查。 – user

0

检查结果:

The result

按照我的代码:

#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 
+0

我更新我的问题。请检查。如果我在'cellForRowAtIndexPath'中使用'UITableViewCellAccessoryNone',这是行不通的。复选标记外观 – user

+0

@你的意思是didselectrow现在不推送到下一个控制器? – aircraft

+0

我更新我的问题。请检查。 – user

0

如果fileExists,请在viewWillAppear中重新加载您的tableView。使用模型来标记fileExists单元格。

@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 
+0

我更新我的问题。请检查。 – user