我正在创建一个自定义 UITableViewCell
,其样式与 UITableViewCellStyleValue1
类似,但右侧有一个可编辑文本字段,而不是 detailTextLabel
>。我对如何正确调整编辑字段的大小有点困惑。我想考虑 textLabel
的长度以及 contentView
的大小,并能够在其表格 View 处于编辑状态时做正确的事情模式,或者是否添加了附件 View (如展开箭头)。
但 contentView
的宽度似乎始终为 320(在 iPhone 上的纵向模式下),即使对于分组表格也是如此。在我的 UITableViewCell
子类中,我看不到如何访问它所属的表格 View ,因此我可以获得表格的样式并相应地调整边距。
我想我可以在 tableView 的委托(delegate) -tableView:willDisplayCell:forRowAtIndexPath:
方法中以自定义方式处理这个问题,但这完全违背了拥有可重用单元类的目的。
我一定错过了一些关键概念,但我很茫然。有人吗?
谢谢! 兰迪
最佳答案
您可能想在 UITableViewCell 子类中控制布局。例如:
- (void)layoutSubviews
{
CGRect b = [self bounds];
b.size.width += 30; // allow extra width to slide for editing
b.origin.x -= (self.editing) ? 0 : 30; // start 30px left unless editing
[contentView setFrame:b];
// then calculate (NSString sizeWithFont) and addSubView, the textField as appropriate...
//
[super layoutSubviews];
}
关于iphone - 自定义 UITableViewCell subview 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3730750/