OGeek|极客世界-中国程序员成长平台

标题: objective-c - 如何消除分组UITableView的左右边距 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:21
标题: objective-c - 如何消除分组UITableView的左右边距

如果我有一个分组的 UITableView,它的边缘会带有一个灰色的边框/缓冲区。有什么方法可以知道这个缓冲区有多大,或者等效地,它里面的实际白色单元有多大?



Best Answer-推荐答案


这是消除分组 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)setFrameCGRect)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

关于objective-c - 如何消除分组UITableView的左右边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669678/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4