Ios XCode 6问题-dequeueReusableCellWithIdentifier未返回自定义单元格高度
我有一个UITableViewController,带有两个具有自定义高度的原型单元-一个具有187个点的高度,另一个具有140个点的高度。tableview的默认行高为ser到187 我的cellForRowAtIndexPath如下所示:Ios XCode 6问题-dequeueReusableCellWithIdentifier未返回自定义单元格高度,ios,objective-c,uitableview,heightforrowatindexpath,Ios,Objective C,Uitableview,Heightforrowatindexpath,我有一个UITableViewController,带有两个具有自定义高度的原型单元-一个具有187个点的高度,另一个具有140个点的高度。tableview的默认行高为ser到187 我的cellForRowAtIndexPath如下所示: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.sect
- (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"];
}
}
- (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;
}
}
}
我有一个与RowAtIndexPath匹配的高度,如下所示:
- (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"];
}
}
- (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;
}
}
}
当我运行代码时,单元格高度正确显示。分别为187和140
但是,我想将较大单元格的大小更改为213点。为此,我将自定义表格单元格高度更改为213。问题是,当我运行代码(使用XCode 6)时,表格单元格仍然显示为高度187
我尝试将默认的tableview rowheight属性也更改为213。但我还是得到了187分。事实上,当我看故事板(作为文本)时,任何地方都没有提到187。但似乎XCode正在记住以前的值
我尝试过清理、深度清理、重新启动xcode、从手机中删除应用程序以及删除调试产品。我不能忘记187
这可能是XCode 6中的错误吗?在XCode 5中,这不会发生(已验证)。自定义单元高度在XCode 5中工作正常,但这个问题在我安装XCode 6的第二天就出现了
有没有人能帮我提供一些关于尝试的建议,或者确认我没有发疯
heightForRowAtIndexPath
在cellForRowAtIndexPath
之前调用,因此您永远不能使用它来尝试引用单元格并获取高度。查看具有以下日志行和结果的以下代码:
- (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;
}
这导致:
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
记住这一点,你所使用的方法永远不会起作用
您是否尝试过以下更简单的方法(尽管我个人会使用常量)
这是假设第一部分中的所有单元都是213高,而任何其他部分都是140高,这可能是不正确的
归根结底,我认为最好的方法是从本质上询问您的数据源在给定的
indepath
位置上应该存在什么类型的单元格,因此单元格应该有多高。这看起来像是一个与单元格的制作和高度处理有关的问题,而不是Xcode。如果我没记错的话,heightforrowatinexpath是在创建单元格之前调用的,而不是在创建单元格之后调用的。@theduns-好奇您是否有幸解决了这个问题?我相信我也遇到过同样的情况——我正在尝试在UITableView中使用两个原型单元格,每个单元格的行高不同。起初,我研究了heightForRowAtIndexPath:method,但在iOS8中使用它还没有成功。您是否碰巧找到了一种使用脚本表格视图单元格自定义行高来完成此操作的方法?谢谢@我向苹果公司提交了一份雷达报告,他们说这是一个已经报告的问题。我还没有找到其他解决方法,除了从预XCode6序列图像板复制UITableViewCell,粘贴它,并修改序列图像板xml文件中的三个大小参数。如果你想让我把它作为一种可能的解决办法,请告诉我。@theDuncs至少那个建议为我解决了它。我复制粘贴了一个在Xcode 5中创建的单元格。问题解决了。非常感谢你。希望苹果能尽快解决这个问题。我已经使用HeightForRowatineXpath好几个月了,没有问题。它正确地下拉单元格的自定义高度,其中单元格在情节提要中定义。在您的示例中,我假设您没有在stroyboard中添加重用标识符为“cell”的UITableViewCell。如果您这样做,它应该返回正确的值。这是正确的,我没有使用(通常不使用)故事板。但正如我在回答中所说的,为什么不返回您知道的高度,它们将基于数据源和截面?这样,您甚至不必在单元格存在之前(可能)尝试将其出列。如果使用故事板改变操作顺序,我会非常惊讶,因为我不想将单元格的高度硬编码到代码中。我希望能够改变故事板中单元格的高度,并将改变反映在应用程序中。这是去年在XCode 5中运行良好的功能。虽然我知道您来自何方,但您对高度进行了“硬编码”,一个在代码中,一个在故事板中。我不明白它是如何工作的,因为正如我所说,heightForRow是在cellForRow之前调用的,所以单元格还没有初始化,因此出列单元格不应该工作。好吧,即使单元格是用故事板创建的,情况也是如此,但我绝对可能是错的。你还有Xcode 5吗?你完全错了,迈克。谢谢你的帮助。