2

如果我有一个分组的 UITableView,它的边缘会带有一个灰色的边框/缓冲区。有没有办法知道这个缓冲区有多大,或者等效地,它里面的实际白细胞有多大?

4

1 回答 1

3

这是您可以消除分组 UITableView 的左右边距的方法。只是 UITableViewCell 的子类。请注意,如果您希望边距为 0 以外的值,只需更改 setFrame 方法以满足您的需要。

//In CustomGroupedCell.h
#import <UIKit/UIKit.h>

@interface CustomGroupedCell : UITableViewCell
@property (nonatomic, weak) UITableView *myTableView;
@end




//In CustomGroupedCell.m
#import "CustomGroupedCell.h"

@implementation CustomGroupedCell
@synthesize myTableView = _myTableView;

- (void)setFrame:(CGRect)frame 
{
    frame = CGRectMake(- [self cellsMargin] , frame.origin.y, self.myTableView.frame.size.width + 2 *[self cellsMargin], frame.size.height);
    self.contentView.frame = frame;
    [super setFrame:frame];
}

- (CGFloat)cellsMargin {

    // No margins for plain table views
    if (self.myTableView.style == UITableViewStylePlain) {
        return 0;
    }

    // iPhone always have 10 pixels margin
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        return 10;
    }

    CGFloat tableWidth = self.myTableView.frame.size.width;

    // Really small table
    if (tableWidth <= 20) {
        return tableWidth - 10;
    }

    // Average table size
    if (tableWidth < 400) {
        return 10;
    }

    // Big tables have complex margin's logic
    // Around 6% of table width,
    // 31 <= tableWidth * 0.06 <= 45

    CGFloat marginWidth  = tableWidth * 0.06;
    marginWidth = MAX(31, MIN(45, marginWidth));
    return marginWidth;
}

@end
于 2012-07-26T17:07:52.673 回答