如何消除分组UITableView的左右边距

如果我有一个分组的UITableView,它的边缘有一个灰色的边框/缓冲区。 有没有办法告诉这个缓冲区有多大,或者等价,它里面的实际白色单元有多大?

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

//In CustomGroupedCell.h #import  @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