[英]ios tableView reloadRowsAtIndexPaths not working
我有一個問題,我有一個UITableViewController
,當視圖出現時,我進行一些異步計算,這將導致更新表中的特定行。
在viewWillAppear
函數內部,我計算了需要更新的必要行,如下所示:
- (void)reloadPaths:(NSMutableDictionary *)bookingsDict
{
NSMutableArray* indexArray = [NSMutableArray array];
int i = 0;
for (NSString *dateKey in self.eventsToShow) {
NSMutableDictionary *venues = [bookingsDict objectForKey:dateKey];
if (venues) {
int j = 0;
NSArray *events = [self.eventsToShow objectForKey:dateKey];
for (DCTEvent *event in events) {
NSString *venueIdString = [NSString stringWithFormat:@"%@", event.eventVenueId];
if ([venues objectForKey:venueIdString]) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
[indexArray addObject:indexPath];
}
j++;
}
}
i++;
}
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
但是我注意到,之后從不調用cellForRowAtIndexPath
函數,並且單元格沒有正確更新。 知道問題可能是什么嗎?
編輯:我剛剛注意到,如果我滾動出應該更新的單元格的視圖,那么當我滾動回視圖時它會更新。 在視野中有沒有辦法讓它更新?
包裹它怎么樣?
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
以下是我發現的一些類似問題。
在reloadRowsAtIndexPaths之后不調用cellForRowAtIndexPath
UITableView的reloadRowsAtIndexPaths:(NSArray *)indexPaths除非你調用它兩次,否則無法重新加載?
希望這可以幫助。
編輯 :我認為你沒有得到indexPath檢查部分可能是不變的,因為你在每個對象被遍歷時增加:
- (void)reloadPaths:(NSMutableDictionary *)bookingsDict
{
NSMutableArray* indexArray = [NSMutableArray array];
int i = 0;
for (NSString *dateKey in self.eventsToShow) {
NSMutableDictionary *venues = [bookingsDict objectForKey:dateKey];
if (venues) {
int j = 0;
NSArray *events = [self.eventsToShow objectForKey:dateKey];
for (DCTEvent *event in events) {
NSString *venueIdString = [NSString stringWithFormat:@"%@", event.eventVenueId];
if ([venues objectForKey:venueIdString]) {
//changed here as section might be constant as i think it might be
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:0];
[indexArray addObject:indexPath];
}
j++;
}
}
i++;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
試試這個 :
//your code here
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
對我來說,將重載代碼推送到主隊列解決了問題
DispatchQueue.main.async {
self.tableView.reloadRows(at: [indexPath], with: .automatic)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.