我试图调整我的表格视图中的一个单元格的高度。我正在调整单元格的“大小检查器”内的“行高”设置的大小。当我在我的iPhone上运行应用程序时,单元格的默认大小设置自表格视图中的“行大小”。
如果我改变了表格视图的“行大小”,那么所有单元格的大小都会改变。我不想这样做,因为我只想为一个单元格自定义大小。我已经看到了很多关于这个问题的程序化解决方案的帖子,但如果可能的话,我更喜欢通过故事板来实现。
我试图调整我的表格视图中的一个单元格的高度。我正在调整单元格的“大小检查器”内的“行高”设置的大小。当我在我的iPhone上运行应用程序时,单元格的默认大小设置自表格视图中的“行大小”。
如果我改变了表格视图的“行大小”,那么所有单元格的大小都会改变。我不想这样做,因为我只想为一个单元格自定义大小。我已经看到了很多关于这个问题的程序化解决方案的帖子,但如果可能的话,我更喜欢通过故事板来实现。
当前回答
实际上有两个地方需要更改行高,第一个是单元格(您已经更改了它) 现在选择表格视图并检查大小检查器
其他回答
对于动态单元格,UITableView上设置的rowHeight总是覆盖单个单元格的rowHeight。
在我看来,这种行为是个bug。任何时候你都必须在两个地方管理UI,这很容易出错。例如,如果您在故事板中更改单元格大小,您必须记住在highightforrowatindexpath:中也要更改它们。在苹果修复这个错误之前,目前最好的解决方法是重写highightforrowatindexpath:,但是使用故事板中的实际原型单元格来确定高度,而不是使用神奇的数字。这里有一个例子:
- (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:故事板。
如果你想设置一个静态行高,你可以这样做:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
作为评论添加,但作为可见性的答案发布:
如果最初将表视图设置为“静态单元格”,然后将其更改为“动态原型”,似乎也会出现问题。我有一个问题,甚至委托方法heightForRowAtIndexPath被忽略。我首先通过直接编辑故事板XML来解决这个问题,然后最终从头开始重新构建故事板场景,从一开始就使用动态原型。
您可以使用具有自定义高度的原型单元格,然后调用cellForRowAtIndexPath:并返回其frame.height.:。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView
cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
鉴于我没有通过Interface Builder找到这个问题的任何解决方案,我决定在Swift中使用两个动态单元发布一个编程解决方案,即使最初的问题要求通过Interface Builder解决问题。不管怎样,我认为这对Stack Overflow社区是有帮助的:
import UIKit
enum SignInUpMenuTableViewControllerCellIdentifier: String {
case BigButtonCell = "BigButtonCell"
case LabelCell = "LabelCell"
}
class SignInUpMenuTableViewController: UITableViewController {
let heightCache = [SignInUpMenuTableViewControllerCellIdentifier.BigButtonCell : CGFloat(50),
SignInUpMenuTableViewControllerCellIdentifier.LabelCell : CGFloat(115)]
private func cellIdentifierForIndexPath(indexPath: NSIndexPath) -> SignInUpMenuTableViewControllerCellIdentifier {
if indexPath.row == 2 {
return SignInUpMenuTableViewControllerCellIdentifier.LabelCell
} else {
return SignInUpMenuTableViewControllerCellIdentifier.BigButtonCell
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.heightCache[self.cellIdentifierForIndexPath(indexPath)]!
}
...
}