[英]XCode 6 issue - dequeueReusableCellWithIdentifier not returning custom cell height
I have a UITableViewController with 2 prototype cells with custom heights - one has a height of 187 points and the other has a height of 140 points. 我有一个带有2个具有自定义高度的原型单元格的UITableViewController-一个高度为187点,另一个高度为140点。 The tableview's default row height is ser to 187. 表格视图的默认行高为187。
My cellForRowAtIndexPath looks like this: 我的cellForRowAtIndexPath看起来像这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
MyListingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingCell"];
[cell initWithListing:[self.listings objectAtIndex:indexPath.row]];
return cell;
} else {
return [tableView dequeueReusableCellWithIdentifier:@"AddMyListingCell"];
}
}
and I have a matching heightForRowAtIndexPath which looks like this: 并且我有一个匹配的heightForRowAtIndexPath,看起来像这样:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingCell"];
return cell.frame.size.height;
} else {
//if no listings exist, make the cell full-screen height
if (self.listings.count == 0) {
return tableView.frame.size.height;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddMyListingCell"];
return cell.frame.size.height;
}
}
}
When I run the code, the cell heights come out correctly. 当我运行代码时,单元格高度正确显示。 187 and 140 respectively. 分别为187和140。
However, I would like to change the size of the larger cell to 213 points. 但是,我想将较大的单元格的大小更改为213点。 To do this I change the custom tablecell height to 213. The problem is that when I run the code (using XCode 6) the table cells still come out as height 187. 为此,我将自定义表格单元格的高度更改为213。问题是,当我运行代码(使用XCode 6)时,表格单元格仍然以高度187出现。
I tried changing the default tableview rowheight property, also to 213. However I still get 187. In fact, when I look at the storyboard (as text) there is no mention of 187 anywhere. 我尝试将默认的tableview rowheight属性也更改为213。但是,我仍然得到187。实际上,当我查看情节提要(作为文本)时,在任何地方都没有提到187。 But it seems that XCode is remembering the previous value. 但是似乎XCode记住了以前的值。
I have tried cleaning, deep cleaning, restarting xcode, deleteing the app from my phone, and deleting my debug products. 我尝试了清洁,深度清洁,重新启动xcode,从手机中删除应用程序以及删除调试产品。 I can't get it to forget the 187. 我无法忘记187。
Could this be a bug in XCode 6? 这可能是XCode 6中的错误吗? In XCode 5 this does not happen (proven). 在XCode 5中不会发生这种情况(已证明)。 Custom cell heights have been working fine in XCode 5 but this problem has arisen the very day after I install XCode 6. 自定义单元格高度在XCode 5中可以正常工作,但是在安装XCode 6之后的第二天就出现了此问题。
Can anyone help with some advice on things to try, or some confirmation that I'm not going crazy. 任何人都可以在尝试尝试方面提供一些建议,或者可以确认我没有发疯。
heightForRowAtIndexPath
is called BEFORE cellForRowAtIndexPath
, so you can never use it to try to reference a cell and get the height. heightForRowAtIndexPath
称为cellForRowAtIndexPath
,因此您永远不能使用它来尝试引用单元格并获取高度。 Look at the following code with the following log lines and result: 查看带有以下日志行和结果的以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"CELL");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"HEIGHT");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
NSLog(@"%@", cell);
return cell.frame.size.height;
}
That results in: 结果是:
2014-09-23 09:55:05.114 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.114 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.115 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.115 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.115 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.116 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.116 NewTesting[54040:60b] HEIGHT
2014-09-23 09:55:05.116 NewTesting[54040:60b] (null)
2014-09-23 09:55:05.117 NewTesting[54040:60b] CELL
2014-09-23 09:55:05.118 NewTesting[54040:60b] CELL
2014-09-23 09:55:05.119 NewTesting[54040:60b] CELL
2014-09-23 09:55:05.120 NewTesting[54040:60b] CELL
With that in mind the method you're using would never work. 考虑到这一点,您使用的方法将永远无法使用。
Have you tried something more simple, like the following (although I would personally use constants) 您是否尝试过以下更简单的方法(尽管我个人会使用常量)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath.section == 0 ? 213 : 140;
}
That is assuming all the cells in the first section are 213 tall and any other section 140 tall, which may be incorrect. 那就是假设第一部分中的所有单元格都高213,其他任何部分140都高,这可能是不正确的。
Ultimately, I think the best way to do what you want to do is to essentially ask your data source what type of cell should exist at that given indexPath
, and therefore how tall the cell should be. 最终,我认为做您想要做的事情的最佳方法是本质上询问您的数据源,在给定的indexPath
应该存在哪种类型的单元格,因此单元格应该有多高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.