简体   繁体   English

在调用 reloadData 时调用 numberOfRowsInSection 后有时不调用 cellForRowAtIndexPath

[英]cellForRowAtIndexPath is not called sometimes after calling numberOfRowsInSection on calling reloadData

I have a simple application where I have a table view & depending on the callback with array I reload the table data using reloadData method.我有一个简单的应用程序,其中有一个表格视图,并且取决于带有数组的回调,我使用reloadData方法重新加载表格数据。 Initially for first 5-10 times of loading this table view works without any problem.最初加载此表视图的前 5-10 次工作没有任何问题。 After playing with the application for sometimes I noticed that the table view is blank because of the reason that cellForRowAtIndexPath is not called by the framework.在玩了一段时间后,我注意到表格视图是空白的,因为框架没有调用cellForRowAtIndexPath I verified that datasource & delegate is set properly & numberOfRowsInSection is returning a valid positive number.我验证了datasourcedelegate设置正确,并且numberOfRowsInSection返回了一个有效的正数。 Once I see the blank table view then I can see this blank view repeatedly until I exit the application.一旦我看到空白表视图,我就可以反复看到这个空白视图,直到我退出应用程序。 I do not get any error or warnings in the console.我在控制台中没有收到任何错误或警告。 I tried to tweak the array but to no avail.我试图调整阵列但无济于事。

Here is the code:这是代码:

- (void)notifyArrayLoaded:(NSArray *)arr {
    NSUInteger capacity = [arr count];
    _tbDataArray = [[NSMutableArray alloc] initWithCapacity:capacity];

    // _tbDataArray is filled with the data from arr

    // Reload the table data
    [_tableView reloadData]; 
}

Any clue will be highly appreciated.任何线索将不胜感激。

Thanks in advance.提前致谢。 Sumit苏米特

Hi DeanWombourne, I do not have viewDidUnLoad method but I have viewWillDisAppear method which I am posting it here.嗨 DeanWombourne,我没有viewDidUnLoad方法,但我有viewWillDisAppear方法,我将其发布在这里。

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];

    view.transform = CGAffineTransformIdentity;
    view.transform = CGAffineTransformMakeRotation(0);
    view.bounds = CGRectMake(0.0, 0.0, 320, 480);
}

This is the logs of tableView as logged.这是记录的tableView的日志。

[numberOfSectionsInTableView:] [Line 223] <UITableView: 0xa5fc00; frame = (0 0; 320 0); clipsToBounds = YES; layer = <CALayer: 0x4830b0>; contentOffset: {0, 0}>
[tableView:numberOfRowsInSection:] [Line 229] <UITableView: 0xa5fc00; frame = (0 0; 320 0); clipsToBounds = YES; layer = <CALayer: 0x4830b0>; contentOffset: {0, 0}>
<UITableView: 0xa5fc00; frame = (0 0; 320 0); clipsToBounds = YES; layer = <CALayer: 0x4830b0>; contentOffset: {0, 0}>
[numberOfSectionsInTableView:] [Line 223] <UITableView: 0xa5fc00; frame = (0 0; 320 0); clipsToBounds = YES; layer = <CALayer: 0x4830b0>; contentOffset: {0, 0}>
[tableView:numberOfRowsInSection:] [Line 229] <UITableView: 0xa5fc00; frame = (0 0; 320 0); clipsToBounds = YES; layer = <CALayer: 0x4830b0>; contentOffset: {0, 0}>

Make sure you call reloadData on the main thread.确保在主线程上调用 reloadData。 If you are loading your data asynchronously using performSelectorInBackground and calling reloadData in that method, you should, instead of doing something like:如果您使用performSelectorInBackground异步加载数据并在该方法中调用reloadData ,您应该,而不是执行以下操作:

[tableViewController reloadData]

What you should be doing is你应该做的是

[tableViewController performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]

If reloadData causes numberOfRowsInSection to be called but not cellForRowAtIndexPath , check that the UITableView instance you called reloadData on is the same UITableView that you have displayed in your view, eg如果reloadData导致调用numberOfRowsInSection而不是cellForRowAtIndexPath ,请检查您调用reloadDataUITableView实例是否与您在视图中显示的UITableView相同,例如

NSLog(@"Calling reloadData on %@", self.tableView);
[self.tableView reloadData];

I think you'll find you have two different UITableViews .我想你会发现你有两个不同的UITableViews The puzzle then is working out why...然后谜题就是为什么……

This may be too late, but anyways this might help.I have faced the same problem.这可能为时已晚,但无论如何这可能会有所帮助。我遇到了同样的问题。

In short may be "Florian Segginger's" answer was right.简而言之,“Florian Segginger”的答案可能是正确的。

My situation was 1.calling web service asynchronously to get data (pull to refresh) 2.While this was processing, moved to another view controller (pushed) 3.poped back and my app crashed.我的情况是 1. 异步调用 web 服务以获取数据(拉动刷新) 2. 在处理过程中,移动到另一个视图 controller (推送) 3. 弹回来,我的应用程序崩溃了。

So in between steps 2 and 3, I noticed that number of rows and number of sections for table is actually called but cell for row did not.It resumed when I popped back by the time the count of the tablecell changed due to another logic.Handled this by testing, if view is visible in the connection did finish loading since I was reloading table in view will appear.This might be due to cell for row is actually a rendering(UI) action which should run on main thread.因此,在第 2 步和第 3 步之间,我注意到实际上调用了表的行数和节数,但没有调用行的单元格。当我在 tablecell 的计数由于另一个逻辑而改变时弹出回来时,它恢复了。通过测试处理此问题,如果视图在连接中可见,则确实完成加载,因为我正在重新加载视图中的表。这可能是由于行的单元格实际上是应该在主线程上运行的渲染(UI)操作。

I had the same symptoms too.我也有同样的症状。 In my case, the first time I loaded the data (from core data) in viewDidLoad, NSSortDescriptor was used to sort the data.就我而言,我第一次在 viewDidLoad 中加载数据(来自核心数据)时,使用 NSSortDescriptor 对数据进行排序。

On the click of a button, the core data was fetched again (this time with changes) and tableView data reloaded.点击一个按钮,核心数据被再次获取(这次有变化)并重新加载了 tableView 数据。 It initially gave me a blank table view after the button was clicked because I forgot to sort the data the second time I fetched it.单击按钮后,它最初给了我一个空白表格视图,因为我第二次获取数据时忘记对数据进行排序。

Learning points: Remember to call all methods which modify the cell like NSSortDescriptor if you have used them in the beginning!学习要点:如果你一开始使用过NSSortDescriptor等修改单元格的方法,记得调用它们!

go to the header file of the viewcontroller and add (IBOutlet) to the tableView member. go 到 viewcontroller 的 header 文件,并将 (IBOutlet) 添加到 tableView 成员。 then connect throught interfaceb然后通过接口b连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在AppDelegate中的reloadData之后未调用cellForRowAtIndexPath - cellForRowAtIndexPath not called after reloadData in AppDelegate iOS:在numberOfRowsInSection之后没有立即调用cellForRowAtIndexPath - iOS: cellForRowAtIndexPath not being called immediately after numberOfRowsInSection numberOfRowsInSection:未在 reloadData 上调用 - numberOfRowsInSection: not called on reloadData 选择器在numberOfRowsInSection中被调用,但不在cellForRowAtIndexPath中被调用 - Selector gets called in numberOfRowsInSection but not in cellForRowAtIndexPath 调用reloadData后NSArray为空 - NSArray Empty After Calling reloadData cellForRowAtIndexPath:被立即调用,而numberOfRowsInSection:不被调用 - cellForRowAtIndexPath: being called immediately while numberOfRowsInSection: is not called tableView reloadData不起作用:未调用cellForRowAtIndexPath - tableView reloadData not working: cellForRowAtIndexPath not called 在tableView reloadData上未调用cellForRowAtIndexPath - cellForRowAtIndexPath not being called on tableView reloadData 在Apple的MultipleDetailViews示例中调用numberOfRowsInSection但不调用cellForRowAtIndexPath - numberOfRowsInSection called but not cellForRowAtIndexPath in MultipleDetailViews sample of apple UITableView 为空,即使在调用 reloadData 之后 - UITableView empty, even after calling reloadData
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM