2016-12-04 5 views
0

Ich habe einen TableView. Wenn ich auf die Zelle klicke, wird die Datei mit der Animation UIActivityIndicator heruntergeladen. Sobald der Download abgeschlossen ist, wird das Häkchen angezeigt (die Datei existiert) und der Benutzer kann zum nächsten Controller wechseln. Nach dem Wechsel zum nächsten Controller und Zurückkehren sind alle Häkchen verschwunden. Wie es geht?Häkchen entfernen

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

Können Sie bitte weiteren Code wie 'cellForRowAtindexPath' und' didSelectRowAtIndexPath' hinzufügen. –

+0

@Benutzer überprüfen Sie die Antwort, war dies Ihre Anforderung? – aircraft

+0

@NiravD Ich aktualisiere meine Frage. Bitte prüfe. – user

Antwort

1

neu laden Ihre Tableview in viewWillAppear

+0

'- (void) viewWillAppear: (BOOL) animiert { [self.tableView reloadData]; } 'Ich füge Code hinzu, aber es funktioniert nicht – user

+0

noch einmal überprüfen Sie Ihr TableView-Objekt, Verbindung gegeben oder nicht. – Himanth

+0

Ich aktualisiere meine Frage. Bitte prüfe. – user

0

Überprüfen Sie das Ergebnis:

The result

mein Code wie folgt vor:

#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

Ich aktualisiere meine Frage. Bitte prüfe. Wenn ich 'UITableViewCellAccessoryNone' in' cellForRowAtIndexPath' verwende, funktioniert es nicht. Häkchen Aussehen – user

+0

@Benutzer meinen Sie didselectrow nicht Push to nextcontroller jetzt? – aircraft

+0

Ich aktualisiere meine Frage. Bitte prüfe. – user

0

Wenn fileExists, laden Sie Ihre tableView in viewWillAppear neu. Verwenden Sie ein Modell, um die Zelle fileExists zu markieren.

@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

Ich aktualisiere meine Frage. Bitte prüfe. – user

Verwandte Themen