UITableView reloadData - 未触发cellForRowAtIndexPath

Bor*_*zin 8 iphone uitableview reloaddata

我有splitViewController,它具有MasterViewController的一些viewController和DetailViewController的一些tableViewController.当我按下masterViewController上的一个按钮时,我想在detailViewController中显示新的tableViewController而不是现有的.

所以我喜欢这样:

SearchDetailViewController *searchDetailViewController = [[SearchDetailViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *searchDetailNavigationController = [[UINavigationController alloc] initWithRootViewController:searchDetailViewController];
Run Code Online (Sandbox Code Playgroud)

之后,我将数据传递给new tableController中显示:

[searchDetailViewController passInSearchResults:listOfItems];
Run Code Online (Sandbox Code Playgroud)

之后我将"新控制器""推送"到splitViewController:

[searchSplitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, searchDetailNavigationController, nil]];
Run Code Online (Sandbox Code Playgroud)

在目标tableViewController方法中传递"passInSearchResults"数据,我也调用reloadData.方法看起来像这样:

- (void)passInSearchResults:(NSMutableDictionary *)searchResults {
    self.searchResultList = searchResults;
    NSLog(@"Data is passed in: %@",self.searchResultList);
    [self.tableView reloadData];
    //[self.tableView setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)

控制台:传入数据:[这里我得到了我想要的确切数据,看起来恰到好处].

After this I see that method "numberOfRowsInSection" is fired:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Setting table length: %i",[self.searchResultList count]);
    return [self.searchResultList count];
}
Run Code Online (Sandbox Code Playgroud)

控制台:设置表长度:[这里我得到适当的长度]

问题是表没有填充传递的数据,并且没有调用方法"cellForRowAtIndexPath".

怎么可能在reloadData方法"numberOfRowsInSection"被触发但不是方法"cellForRowAtIndexPath"...... ???

谢谢

小智 10

在通话时检查你是否在主线程中 [self.tableView reloadData];

- (void)passInSearchResults:(NSMutableDictionary *)searchResults {
 if ([NSThread isMainThread]) {
    self.searchResultList = searchResults;
    NSLog(@"Data is passed in: %@",self.searchResultList);
    [self.tableView reloadData];
}
else {
    [self performSelectorOnMainThread:@selector(passInSearchResults:) searchResults waitUntilDone:NO];
}
Run Code Online (Sandbox Code Playgroud)

}


Buc*_*non 4

怎么可能在 reloadData 上触发方法“numberOfRowsInSection”而不是方法“cellForRowAtIndexPath”...???

在花了一天时间解决相同的问题后,我认为我们在这里发现了一个错误。不过我确实找到了一个黑客/解决方法。

如果我更改 cellForRowAtIndexPath 方法中使用的数组(添加/删除值或再次完全构建它),则当您重新加载数据时,它似乎会再次正确调用 cellForRowAtIndexPath。

例如,在我的 cellForRowAtIndexPath 中,我有类似的内容:

cell.textLabel.text = [[[self.searchResultsArrayWithSections objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row];

    return cell;
Run Code Online (Sandbox Code Playgroud)

我还有一个方法来构建这个在 viewDidLoad 上调用的数组:

    - (NSMutableArray *)splitIntoSectionsWithAlphabet:(NSMutableArray *)myArray 
{    
NSString *letters = @"abcdefghijklmnopqrstuvwxyz";
NSMutableArray *content = [NSMutableArray new];

//code to build array here, nothing special really

return content;
}
Run Code Online (Sandbox Code Playgroud)

所以我注意到,如果我在调用 reloadData 之前在 searchResultsArrayWithSections 数组上再次专门调用此方法,它实际上会正确调用所有 UITableViewDelegate 方法!(就像我说的,破解但它有效!)

//dirty hack, bug with cancel button and not realoding data, seems we have to modify this array before it will work correctly
self.customerListArrayWithSections = [self splitIntoSectionsWithAlphabet:self.customerListArray];
[self.customerListTv reloadData];
Run Code Online (Sandbox Code Playgroud)