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