cellForRowAtIndexPath:未调用 [英] cellForRowAtIndexPath: not called

查看:95
本文介绍了cellForRowAtIndexPath:未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有两种状态:登录但未登录,我有以下架构(大大简化):

- ViewController A包含搜索框和表格视图。

- ViewController B,用于登录应用程序。

My app has two states: logged in and not logged in, and I have the following architecture (vastly simplified):
- ViewController A which contains a search box and a table view.
- ViewController B which is used for logging in the app.

流程如下:

- 用户未登录in;

- A被推入堆栈。在 viewWillAppear 中,我检查用户是否已登录,如果是,则表示正在进行异步网络请求,一旦完成,表中将加载来自网络。但是,由于此时用户尚未登录,因此表视图为空;

- 用户点击搜索框;因为他没有登录,B被推(确认后);

- 他在B中成功登录,然后按下弹出B并再次显示A的按钮;

- 在这个时间点,因为他已登录,从 viewWillAppear 我执行异步请求;

- 完成后,我调用<$ c表视图上的$ c> reloadData 。

The flow is the following:
- the user is not logged in;
- A is pushed on the stack. In viewWillAppear I check if the user is logged in and if yes, an async network request is being made and, once that is done, the table is loaded with the data from the network. However since the user is not logged in at this point, the table view is empty;
- the user taps on the search box; because he's not logged in, B is pushed (after a confirmation);
- he logs in successfully in B, then presses a button which pops B and shows A again;
- at this point in time, because he's logged in, from viewWillAppear I do the async request;
- when that is completed, I call reloadData on the table view.

我注意到的是 numberOfRowsInSection:被调用并返回正确的结果,但之后不会调用 cellForRowAtIndexPath:并且表格保持为空。

What I notice is that numberOfRowsInSection: is called and it returns the correct result, however cellForRowAtIndexPath: is NOT called afterwards and the table remains empty.

我已检查并在主线程上调用 reloadData

I've checked and reloadData is called on the main thread.

任何想法是什么?因为它让我疯了!

Any idea what can it be? Cause it's driving me nuts!

谢谢,

S。

Thanks,
S.

编辑:这是A中的 viewWillAppear 代码的异步位。

Here's the async bit of code from viewWillAppear in A.

if ([User isLoggedIn]) {
    [self.asyncRequest fetchDataWithCompletionHandler:^(id response, NSError *error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (error) {
            [Utils displayError:error];
        } else {
            self.array = response;

            self.isLoaded = YES;
            [self.tableView reloadData];
            [self.tableView setContentOffset:CGPointMake(0.0f, 0.0f) animated:NO];
        }
    }];
}

我已检查异步请求是否成功完成且 response 包含正确的数据(这是用于支持 UITableView 的数组)。

I've checked that the async request completes successfully and that response contains the correct data (this is the array used to back the UITableView).

reloadData 之后,我在 tableView中设置了一个断点:numberOfRowsInSection:并在那里停止并返回数组中正确数量的元素。然而,在那之后, tableView:cellForRowAtIndexPath:中的断点永远不会被命中。

After reloadData, I've put a breakpoint in tableView:numberOfRowsInSection: and it stops there and it returns the correct number of elements in array. After that, however, the breakpoint in tableView:cellForRowAtIndexPath: is never hit.

推荐答案

为什么 numberOfRowsInSection 将被调用的一个有效场景,但 cellForRowAtIndexPath 将不会被调用时的大小或表的定位不需要显示任何行。

One valid scenario for why numberOfRowsInSection would be called but cellForRowAtIndexPath would not be called is when the size or positioning of the table does not require any rows to be displayed.

例如,假设您有一个20像素高的表,但第一部分的标题高达30,并且您为标题返回了nil(或者没有实现 viewForHeaderInSection )。在这种情况下,不会显示任何行,看起来表格就不存在。

For example, let's say you had a table that was 20 pixels high, but the header for the first section was 30 high and you returned nil for the header (or did not implement viewForHeaderInSection). In this case, no rows would be displayed and it would look like the table just isn't there.

我看到你正在使用IB。由于假定页眉和页脚大小,因此表格的大小在IB中可能具有欺骗性。我会记录表格的框架,以便了解应用程序运行时的位置(与IB中出现的位置相比)。我还要确保在调用reloadData之前手动调整表格框架的大小和位置。这将解决这种有效的情况,即不调用 cellForRowAtIndexPath

I see you are using IB. The sizing of the table can be deceptive in IB as it is assuming header and footer sizes. I would log the frame for the table so you understand where it is when the appliction is run (versus where it appears in IB). I would also be sure to manually size and position the table's frame before calling reloadData. This will solve this valid case where cellForRowAtIndexPath is not called.

这篇关于cellForRowAtIndexPath:未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆