如何消除分组UITableView的左右边距 [英] How to eliminate the left and right margins of a grouped UITableView

查看:165
本文介绍了如何消除分组UITableView的左右边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a grouped UITableView, it comes with a grayish border/buffer around the edge of it. Is there any way to tell how large this buffer is, or equivalently, how big the actual white cell inside it is?

推荐答案

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

This is how you can eliminate the left and right margins of a grouped UITableView. Just subclass UITableViewCell. Note that if you want the margin to be something other than 0 just change the setFrame method to suit your needs.

//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

这篇关于如何消除分组UITableView的左右边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆