我正在尝试调整表视图中一个单元格的高度。我是通过在该单元格的“大小检查器”内部的“行高度”设置来调整其大小的。但当我在iPhone上运行应用程序时,该单元格具有从表视图的“行高度”设置中设置的默认大小。
如果我改变表视图的“行高度”,则所有单元格的大小都会发生改变。我不想这样做,因为我只想要一个自定义大小的单元格。我看到了很多关于编程解决问题的帖子,但如果可能的话,我更喜欢通过Storyboard来完成。
我正在尝试调整表视图中一个单元格的高度。我是通过在该单元格的“大小检查器”内部的“行高度”设置来调整其大小的。但当我在iPhone上运行应用程序时,该单元格具有从表视图的“行高度”设置中设置的默认大小。
如果我改变表视图的“行高度”,则所有单元格的大小都会发生改变。我不想这样做,因为我只想要一个自定义大小的单元格。我看到了很多关于编程解决问题的帖子,但如果可能的话,我更喜欢通过Storyboard来完成。
如果您使用UITableViewController,请实现此方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
在行的函数中,您可以选择高度。例如:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 100;
}
else {
return 60;
}
}
在这个例子中,第一行的高度为100像素,其他行的高度为60像素。对于动态单元格,UITableView
上设置的 rowHeight
总是覆盖个别单元格的 rowHeight
。
这种行为在我看来是一个错误。每当您必须在两个地方管理 UI 时,它就容易出错。例如,如果您更改了故事板中的单元格大小,则必须记住在 heightForRowAtIndexPath:
中进行更改。在 Apple 修复此问题之前,当前最佳解决方法是重写 heightForRowAtIndexPath:
,但是使用从故事板中实际原型单元格来确定高度,而不是使用 魔数 。以下是示例:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* In this example, there is a different cell for
the top, middle and bottom rows of the tableView.
Each type of cell has a different height.
self.model contains the data for the tableview
*/
static NSString *CellIdentifier;
if (indexPath.row == 0)
CellIdentifier = @"CellTop";
else if (indexPath.row + 1 == [self.model count] )
CellIdentifier = @"CellBottom";
else
CellIdentifier = @"CellMiddle";
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell.bounds.size.height;
}
这将确保在运行时自动捕捉到原型单元格高度的任何更改,您只需要在一个地方管理您的UI:storyboard。
heightForRowAtIndexPath
中调用 dequeueReusableCellWithIdentifier
会对我造成无限递归。 - xaphod我已根据各种答案/评论的提示构建了代码,以便使其适用于使用原型单元格的故事板。
此代码:
感谢Answerbot、Brennan和lensovet。
- (NSString *)cellIdentifierForIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = nil;
switch (indexPath.section)
{
case 0:
cellIdentifier = @"ArtworkCell";
break;
<... and so on ...>
}
return cellIdentifier;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
static NSMutableDictionary *heightCache;
if (!heightCache)
heightCache = [[NSMutableDictionary alloc] init];
NSNumber *cachedHeight = heightCache[cellIdentifier];
if (cachedHeight)
return cachedHeight.floatValue;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
CGFloat height = cell.bounds.size.height;
heightCache[cellIdentifier] = @(height);
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
<... configure cell as usual...>
实际上,你需要在两个地方更改行高度,首先是单元格(你已经更改了它),现在选择表视图并检查大小检查器。
我认为这是一个bug。
尝试直接在Storyboard上通过鼠标拖动来调整高度,而不是使用实用程序检查器。
我用这种方法解决了这个问题。
如果你正在使用Swift,就像这样使用。不要使用storyboard来选择行高度。像这样通过编程方式设置表格行高:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 || indexPath.row == 1{
let cell = self.tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! Section1TableViewCell
self.tableView.rowHeight = 150
cell.label1.text = "hiiiiii"
cell.label2.text = "Huiiilllllll"
return cell
} else {
let cell = self.tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! Section2TableViewCell
self.tableView.rowHeight = 60
cell.label3.text = "llll"
return cell
}
}
cell.sizeToFit(); self.tableView.rowHeight = cell.frame.height
。 - Andy Chou- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
return height;
}
在XML视图中打开Storyboard,尝试编辑所需元素的rowHeight
属性。
当我尝试为我的原型行设置自定义rowHeight
时,这对我有用。虽然通过检查器无法工作,但通过XML可以正常工作。
cellForRowAtIndexPath:
并返回其frame.height
。- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView
cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
UITableView
内容从“动态原型”更改为“静态单元格”,我尝试这样做后,整个项目都崩溃了,差点把我自己也搞垮了。 - abbood