ios 故事板中的自定义单元格行高设置没有响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8615862/
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
Custom Cell Row Height setting in storyboard is not responding
提问by zirinisp
I am trying to adjust the cell height for one of the cells on my table view. I am adjusting the size from the "row height" setting inside the "size inspector" of the cell in question. When I run the app on my iPhone the cell has the default size set from the "row size" in the table view.
我正在尝试调整表格视图中一个单元格的单元格高度。我正在根据相关单元格的“大小检查器”内的“行高”设置调整大小。当我在 iPhone 上运行该应用程序时,单元格的默认大小设置为表视图中的“行大小”。
If I change the "row size" of the table view then the size of all cells changes. I do not want to do that as I want a custom size only for one cell. I have seen a lot of posts that have a programmatic solution to the problem, but I would prefer to do it through storyboard, if that is possible.
如果我更改表视图的“行大小”,则所有单元格的大小都会更改。我不想这样做,因为我只想要一个单元格的自定义大小。我看过很多帖子都提供了解决问题的程序化解决方案,但如果可能的话,我更愿意通过故事板来解决。
回答by pixelfreak
On dynamiccells, rowHeight
set on the UITableView always overrides the individual cells' rowHeight.
在动态单元格上,rowHeight
设置在 UITableView 上总是会覆盖单个单元格的 rowHeight。
But on staticcells, rowHeight
set on individual cells can override UITableView's.
但是在静态单元格上,rowHeight
在单个单元格上设置可以覆盖 UITableView 的。
Not sure if it's a bug, Apple might be intentionally doing this?
不确定这是否是一个错误,Apple 可能是故意这样做的?
回答by Beber
If you use UITableViewController, implement this method:
如果你使用 UITableViewController,实现这个方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
In the function of a row you can choose Height. For example,
在行功能中可以选择高度。例如,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 100;
}
else {
return 60;
}
}
In this exemple, the first row height is 100 pixels, and the others are 60 pixels.
本例中,第一行高为100像素,其他为60像素。
I hope this one can help you.
我希望这个可以帮助你。
回答by memmons
For dynamic cells, rowHeight
set on the UITableView
always overrides the individual cells' rowHeight
.
对于动态单元格,rowHeight
设置UITableView
总是覆盖单个单元格的rowHeight
。
This behavior is, IMO, a bug. Anytime you have to manage your UI in two places it is prone to error. For example, if you change your cell size in the storyboard, you have to remember to change them in the heightForRowAtIndexPath:
as well. Until Apple fixes the bug, the current best workaround is to override heightForRowAtIndexPath:
, but use the actual prototype cells from the storyboard to determine the height rather than using magic numbers. Here's an example:
这种行为,IMO,是一个错误。任何时候你必须在两个地方管理你的 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;
}
This will ensure any changes to your prototype cell heights will automatically be picked up at runtime and you only need to manage your UI in one place: the storyboard.
这将确保在运行时自动获取对原型单元格高度的任何更改,并且您只需要在一个地方管理您的 UI:故事板。
回答by JosephH
I've built the code the various answers/comments hint at so that this works for storyboards that use prototype cells.
我已经构建了各种答案/评论提示的代码,以便这适用于使用原型单元格的故事板。
This code:
这段代码:
- Does not require the cell height to be set anywhere other than the obvious place in the storyboard
- Caches the height for performance reasons
- Uses a common function to get the cell identifier for an index path to avoid duplicated logic
- 不需要在故事板中的明显位置以外的任何地方设置单元格高度
- 出于性能原因缓存高度
- 使用通用函数获取索引路径的单元格标识符以避免重复逻辑
Thanks to Answerbot, Brennan and lensovet.
感谢 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...>
回答by Kristjan H.
There are actually two places where you need to change to row height, first the cell (you already did change that) and now select the Table View and check the Size Inspector
实际上有两个地方你需要改变行高,首先是单元格(你已经改变了),现在选择表视图并检查大小检查器
回答by Fogni
I think it is a bug.
我认为这是一个错误。
Try adjust height not by Utility inspector but by mouse drag on the storyboard directly.
尝试不通过实用工具检查器而是通过鼠标直接在情节提要上拖动来调整高度。
I solved this problem with this method.
我用这种方法解决了这个问题。
回答by Chathuranga Silva
If you're using swift , use like this. Don't Use storyboard to select row height. Programmatically set table row height like this,
如果您使用的是 swift ,请像这样使用。不要使用故事板来选择行高。像这样以编程方式设置表格行高,
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
}
}
回答by Hiren Panchal
You can get height of UITableviewCell (in UITableviewController - static cells) from storyboard with help of following lines.
您可以在以下几行的帮助下从情节提要中获取 UITableviewCell 的高度(在 UITableviewController 中 - 静态单元格)。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
return height;
}
回答by Henrik Hartz
You can use prototype cells
with a custom height
, and then invoke cellForRowAtIndexPath:
and return its frame.height
.:.
您可以将原型cells
与自定义一起使用height
,然后调用cellForRowAtIndexPath:
并返回其frame.height
.:。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView
cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
回答by Shtirlic
Open the storyboard in the XML view and try to edit the rowHeight
attribute of the wanted element.
在 XML 视图中打开故事板并尝试编辑rowHeight
所需元素的属性。
It worked for me when I tried to set custom rowHeight for my prototyped row. It's not working via inspector, but via XML it works.
当我尝试为原型行设置自定义 rowHeight 时,它对我有用。它不是通过检查器工作,而是通过 XML 工作。