一个不整洁的解决方案是使表格视图宽 340 像素,离屏幕左边缘 10 像素。
涉及更改私有类属性的解决方案是创建一个UITableViewCell
子类并覆盖其layoutSubviews
方法。当我记录子视图时,我发现这些:
"<UIGroupTableViewCellBackground: 0x95246b0; frame = (9 0; 302 45); autoresize = W; layer = <CALayer: 0x95226b0>>",
"<UITableViewCellContentView: 0x92332d0; frame = (10 0; 300 43); layer = <CALayer: 0x9233310>>",
"<UIView: 0x95248c0; frame = (10 0; 300 1); layer = <CALayer: 0x951f140>>"
如果我们采用这些子视图并填充可用的整个边界会发生什么?
- (void)layoutSubviews;
{
// By default the cell bounds fill the table width, but its subviews (containing the opaque background and cell borders) are drawn with padding.
CGRect bounds = [self bounds];
// Make sure any standard layout happens.
[super layoutSubviews];
// Debugging output.
NSLog(@"Subviews = %@", [self subviews]);
for (UIView *subview in [self subviews])
{
// Override the subview to make it fill the available width.
CGRect frame = [subview frame];
frame.origin.x = bounds.origin.x;
frame.size.width = bounds.size.width;
[subview setFrame:frame];
}
}
在这个特殊的时刻,在 iOS 5.1 模拟器上,这是可行的。现在,一些未来版本的 iOS 可能会重组这些类,导致这种方法灾难性地破坏表格布局。您的应用可能会因更改 UITableViewCellContentView 的属性而被拒绝...即使您只是修改其框架。那么,您需要多少才能让单元格填充表格宽度?