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

Sum*_*mit 5 iphone cocoa-touch uitableview

我有一个简单的应用程序,我有一个表视图和依赖于数组的回调我使用reloadData方法重新加载表数据.最初加载前5-10次这个表视图没有任何问题.在使用应用程序之后,有时我注意到表视图是空白的,因为cellForRowAtIndexPath框架没有调用它.我验证了datasource&delegate设置正确并numberOfRowsInSection返回有效的正数.一旦我看到空白表视图,我就可以反复看到这个空白视图,直到我退出应用程序.我在控制台中没有收到任何错误或警告.我试图调整阵列但无济于事.

这是代码:

- (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]; 
}
Run Code Online (Sandbox Code Playgroud)

任何线索都将受到高度赞赏.

提前致谢.萨米特

嗨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);
}
Run Code Online (Sandbox Code Playgroud)

这是记录的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}>
Run Code Online (Sandbox Code Playgroud)

小智 10

确保在主线程上调用reloadData.如果使用performSelectorInBackground和调用reloadData该方法异步加载数据,则应该执行以下操作:

[tableViewController reloadData]
Run Code Online (Sandbox Code Playgroud)

你应该做的是

[tableViewController performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
Run Code Online (Sandbox Code Playgroud)


Nes*_*tor 5

如果要调用reloadData原因numberOfRowsInSection但是没有cellForRowAtIndexPath,请检查UITableView您调用的实例是否reloadDataUITableView您在视图中显示的实例相同,例如

NSLog(@"Calling reloadData on %@", self.tableView);
[self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

我想你会发现你有两个不同的UITableViews.然后拼图解决了为什么......