您的位置:首页 > 移动开发 > IOS开发

ios 开发自定义cell

2015-11-05 18:49 429 查看
ios 开发自定义cell

1.拦截frame的设置,修改fram

/**
*  拦截frame的设置
*/
- (void)setFrame:(CGRect)frame
{
if (!iOS7) {
CGFloat padding = 10;
frame.size.width += padding * 2;
frame.origin.x = -padding;
}
[super setFrame:frame];
}
2. 分割线

/**
*  初始化分割线
*/
- (void)setupDivider
{
UIView *divider = [[UIView alloc] init];
divider.backgroundColor = [UIColor blackColor];
divider.alpha = 0.2;
[self.contentView addSubview:divider];
self.divider = divider;
}
/**
*  设置子控件的frame
*/
- (void)layoutSubviews
{
[super layoutSubviews];

if (iOS7) return;

// 设置分割线的frame
CGFloat dividerH = 1;
CGFloat dividerW = [UIScreen mainScreen].bounds.size.width;
CGFloat dividerX = 0;
CGFloat dividerY = self.contentView.frame.size.height - dividerH;
self.divider.frame = CGRectMake(dividerX, dividerY, dividerW, dividerH);
}


3.背景

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 初始化操作

// 1.初始化背景
[self setupBg];

// 2.初始化子控件
[self setupSubviews];

// 3.初始化分割线
if (!iOS7) {
[self setupDivider];
}
}
return self;
}

/**
*  初始化子控件
*/
- (void)setupSubviews
{
self.textLabel.backgroundColor = [UIColor clearColor];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
}

/**
*  初始化背景
*/
- (void)setupBg
{
// 设置普通背景
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor whiteColor];
self.backgroundView = bg;

// 设置选中时的背景
UIView *selectedBg = [[UIView alloc] init];
selectedBg.backgroundColor = IWColor(237, 233, 218);
self.selectedBackgroundView = selectedBg;
}


4.cell的最后一行不要分割线

//1.在cell.m中添加属性
@property (nonatomic, assign, getter = isLastRowInSection) BOOL lastRowInSection;

 //2.在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)方法中添加下面一行代码:
cell.lastRowInSection =  (group.items.count - 1 == indexPath.row);

 //3.在cell.m中重写set方法
- (void)setLastRowInSection:(BOOL)lastRowInSection
{
_lastRowInSection = lastRowInSection;

self.divider.hidden = lastRowInSection;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: