2017-06-27 1 views
2

Immer wenn ich reloadRowsAtIndexPaths anrufen; Die Methode didEndDisplayingCell wird zweimal aufgerufen. Warum passiert dies?iOS: Beim Aufruf von reloadRowsAtIndexPaths wird die didEndDisplayingCell zweimal aufgerufen

Ich habe folgende Delegatmethoden von UITableViewDelegate und UITableViewDataSource umgesetzt:

  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

  • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath

Der Grund, warum ich didEndDisplayingCell implementiert habe, ist, dass ich den Firebase-Listener trennen möchte.

Hier ist, was ich in implementiert didEndDisplayingCell

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{ 
// detach the listener for the cell at path index indexPath 

    CustomCell* customCell = (CustomCell*)(cell); 
    [customCell detachListener]; 
} 

Jedes Mal, wenn eine Zelle auf einen aktualisierten Daten hört es ein Verfahren, in dem Viewcontroller feuert die die tableViewCell nachlädt. Hier ist, wie ich diese Methode implementiert:

-(void)refreshCellWithIndexPath:(NSIndexPath*) indexPath andData:(CustomData*)data{ 

    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:data]; 

    [self.tableView beginUpdates]; 
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 
} 

Wenn diese Methode UITableView führt Delegatmethoden wie in der folgenden Reihenfolge aufgerufen werden:

  1. numberOfRowsInSection
  2. cellForRowAtIndexPath
  3. didEndDisplayingCell
  4. didEndDisplayingCell

Warum wird diese didEndDisplayingCell Methode zum zweiten Mal aufgerufen? Bitte helfen Sie mir dabei.

+0

Es wäre interessant, den Wert von 'indexPath' für das Duplikat zu sehen, ruft' didEndDisplayingCell' ... * könnte * Hilfe Punkt zum Grund/Lösung. – DonMag

+0

@DonMag, danke für die schnelle Antwort. Nun ist der 'indexPath' für die doppelten Aufrufe von' didEndDisplayingCell' gleich. –

+0

Hmmm ... macht der IndexPath Sinn? Sieht es so aus, als ob diese Zelle entfernt werden sollte? – DonMag

Antwort

0

Mögliche Abhilfe:

Statt

[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 

Use (swift):

reloadCounter += 1 
CATransaction.begin() 
tableView.beginUpdates() 
CATransaction.setCompletionBlock { 
    self.reloadCounter -= 1 
} 
tableView.reloadRows(at: [IndexPath(row: row, section: 0)], with: .none) 
tableView.endUpdates() 
CATransaction.commit() 

reloadCounter ist ein Ivar.

und in didEndDisplaying:

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if (reloadCounter == 0) { 
     self.output.didEndDisplaying(viewModel: dataProvider.items[indexPath.row]) 
    } 
} 
Verwandte Themen