【发布时间】:2017-11-30 23:11:28
【问题描述】:
每当我打电话给reloadRowsAtIndexPaths ; didEndDisplayingCell 方法被调用了两次。为什么会这样?
我已经实现了以下 UITableViewDelegate 和 UITableViewDataSource 的委托方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
我实现didEndDisplayingCell的原因是,我想分离Firebase监听器。
这是我在里面实现的 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];
}
每当单元格侦听更新的数据时,它都会触发 ViewController 中的一个方法,该方法会重新加载 tableViewCell。以下是我实现该方法的方式:
-(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];
}
当这个方法执行时,UITableView 的委托方法按以下顺序被调用:
numberOfRowsInSectioncellForRowAtIndexPathdidEndDisplayingCelldidEndDisplayingCell
为什么第二次调用这个didEndDisplayingCell 方法?请帮我解决这个问题。
【问题讨论】:
-
看到
indexPath的值对于didEndDisplayingCell的重复调用会很有趣...可能帮助指出原因/解决方案。 -
@DonMag,感谢您的快速回复。对
didEndDisplayingCell的重复调用的indexPath是相同的。 -
嗯... indexPath 有意义吗?看起来那个单元格应该被删除了吗?
-
我传递给
reloadRowAtIndexPath的indexPath,在didEndDisplayingCell的两个调用中得到的都是一样的。 AFAIK 当我们调用reloadRowAtIndexPath时,tableView 会删除该 indexPath 处的单元格并添加新的单元格。但这不应该被调用两次。我还发现didEndDisplayingCell在cellForRowAtIndexPath之后被调用很奇怪。 -
如果删除对
beginUpdates和endUpdates的调用会怎样?
标签: ios objective-c uitableview firebase firebase-realtime-database