an 不整齐的解决方案是使表视图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的属性。所以,你需要多少钱让你的单元格填满桌子宽度?
从设计师的角度来看,这是必须的。如果看起来没有“整洁”的方式,我们会保持宽度并改变背景以适应。如果当时没有其他选项,我会将其标记为最佳答案,谢谢! – d4jo 2012-04-09 17:42:13
这真棒!我一直在寻找这个黑客。我想要一个普通的TableView,但不希望节标题粘到顶端。唯一的方法是切换到分组模式,这会导致UITableView覆盖单元格背景,然后添加此修复程序来调整单元格的大小。恨苹果不给开发人员自由,并让我们做那里黑客定制。 – esbenr 2012-11-21 06:54:58