ios cellForRowAtIndexPath:未调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7712325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cellForRowAtIndexPath: not called
提问by Stelian Iancu
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.
我的应用程序有两种状态:登录和未登录,我有以下架构(大大简化):
- ViewController A,其中包含一个搜索框和一个表视图。
- 用于登录应用程序的 ViewController B。
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.
流程如下:
- 用户未登录;
- A 被压入堆栈。在viewWillAppear
我检查用户是否已登录,如果是,则正在发出异步网络请求,一旦完成,该表将加载来自网络的数据。但是由于此时用户还没有登录,表视图是空的;
- 用户点击搜索框;因为他没有登录,B被推送(确认后);
- 他在 B 中成功登录,然后按下一个按钮,弹出 B 并再次显示 A;
- 在这个时间点,因为他已经登录,viewWillAppear
所以我做了异步请求;
- 完成后,我调用reloadData
表视图。
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.
我注意到它numberOfRowsInSection:
被调用并返回正确的结果,但是cellForRowAtIndexPath:
之后没有调用并且表保持为空。
I've checked and reloadData
is called on the main thread.
我已经检查过并reloadData
在主线程上被调用。
Any idea what can it be? Cause it's driving me nuts!
知道它是什么吗?因为它让我发疯!
Thanks,
S.
谢谢,
S。
EDIT: Here's the async bit of code from viewWillAppear
in A.
编辑:这是 A 中的异步代码viewWillAppear
。
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];
}
}];
}
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
).
我检查了异步请求是否成功完成并且response
包含正确的数据(这是用于支持UITableView
.
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.
之后reloadData
,我tableView:numberOfRowsInSection:
在array
. 然而,在那之后,断点tableView:cellForRowAtIndexPath:
永远不会被击中。
回答by Joel
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.
为什么numberOfRowsInSection
会被调用但cellForRowAtIndexPath
不会被调用的一种有效场景是当表的大小或位置不需要显示任何行时。
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.
例如,假设您有一个 20 像素高的表格,但第一部分的标题是 30 像素高,并且您为标题返回了 nil(或没有实现viewForHeaderInSection
)。在这种情况下,不会显示任何行,并且看起来表格不存在。
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 beforecalling reloadData. This will solve this valid case where cellForRowAtIndexPath
is not called.
我看到你在使用 IB。表格的大小在 IB 中可能具有欺骗性,因为它假设页眉和页脚大小。我会记录表格的框架,以便您了解应用程序运行时的位置(相对于它出现在 IB 中的位置)。我还要确保在调用 reloadData之前手动调整表格框架的大小和位置。这将解决这个cellForRowAtIndexPath
没有被调用的有效情况。
回答by Peter Kelly
Check that the numberOfSectionsInTableView
is not returning 0.
检查numberOfSectionsInTableView
是否没有返回 0。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
回答by Nikolay Dyankov
I had the exact same issue and the problem was that I was trying to call reloadData: from another thread. The solution would be:
我遇到了完全相同的问题,问题是我试图从另一个线程调用 reloadData: 。解决方案是:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
回答by avance
If you are using a different dataSource, as I was, make sure you are retaining the dataSource. Merely instantiating the class that will be the dataSource and assigning it via tableView.dataSource = myDataClass
will not be sufficient as the dataSource property of a tableView is weak and will be released when viewDidLoad completes. All of the other methods were being called for me —?even, surprisingly, heightForRowAtIndexPath
—?so this took me some time to debug.
如果您像我一样使用不同的数据源,请确保保留数据源。仅仅实例化将成为 dataSource 的类并通过分配它tableView.dataSource = myDataClass
是不够的,因为 tableView 的 dataSource 属性很弱,并且会在 viewDidLoad 完成时释放。所有其他方法都被我调用了——?甚至,令人惊讶的是heightForRowAtIndexPath
——?所以这花了我一些时间来调试。
回答by John Erck
// For others showing up on this questions via Google, etc.
// Check and make sure you've set the table view's data source and delegate.
self.tableView.dataSource = self;
self.tableView.delegate = self;
回答by behicsakar
If a Table View inside a view that conflicts something like Scrool View, it does not called. You should separate the views in your Storyboard or *.xib file.
如果视图中的表视图与 Scrool 视图之类的内容发生冲突,则不会调用它。您应该将 Storyboard 或 *.xib 文件中的视图分开。
// True
▼ View
? Table View
? Scrool View
? Constraints
// False
▼ View
? Scrool View
? Table View
? Constraints
回答by Yaroslav
Everyone keeps talking about height, but my TableView
in a StackView
with leading alignment ended up with 0 width.
每个人都在谈论高度,但我TableView
在StackView
前导对齐中的宽度为 0。
Make sure to check if your TableView
is correct size using Debug View Hierarchy
.
确保TableView
使用Debug View Hierarchy
.
回答by user1039695
I solved the problem because my Subview on which i added the UITableView
was not allocated so it was returning nill and tableview was not calling cellForRowAtIndexPath
but numberOfRowsInSection
getting called
我解决了这个问题,因为我添加的子视图UITableView
没有分配,所以它返回 nill 并且 tableview 没有调用cellForRowAtIndexPath
而是numberOfRowsInSection
被调用
回答by Anuran Barman
If you are using auto layout like me and adding tableview to your view controller's view make sure you have added this line when you are allocating your table view.
如果您像我一样使用自动布局并将 tableview 添加到视图控制器的视图中,请确保在分配 table view 时添加了这一行。
tableView?.translatesAutoresizingMaskIntoConstraints = false
Silly mistake from my side.
我这边的愚蠢错误。
回答by TK189
I am using a customer datasource and it was released. Because it is a weak reference of tableview.
我正在使用客户数据源,它已发布。因为它是tableview的弱引用。