ios6/7 自定义cell及内部子控件的适配

适配ios6和ios7 cell宽度问题:

方法一:

 1 - (void)setFrame:(CGRect)frame
 2 {
 3     if (iOS7){
 4         [super setFrame:frame];
 5         return;
 6     }
 7     
 8     CGRect cellF = frame;
 9     cellF.origin.x -= 10;
10     cellF.size.width = [UIScreen mainScreen].bounds.size.width + 10 * 2;
11     frame = cellF;
12     [super setFrame:frame];
13     
14 }

 

方法二: 

 1 // 需要调节cell内部子控件的frame,在layoutSubviews方法最可靠\最有效
 2 - (void)layoutSubviews
 3 {
 4     [super layoutSubviews];
 5     
 6     // 0.设置分隔线的位置
 7     _divider.frame = CGRectMake(self.textLabel.frame.origin.x, 0, self.contentView.frame.size.width + 100, 1.2);
 8     
 9     if (iOS7) return;
10     
11     // 1.cell的frame
12     CGRect cellF = self.frame;
13     cellF.origin.x = -10;
14     CGFloat deltaW = 10 * 2;
15     cellF.size.width = [UIScreen mainScreen].bounds.size.width + deltaW;
16     self.frame = cellF;
17     
18     // 2.右边子控件的frame
19     CGRect accessF = self.accessoryView.frame;
20     accessF.origin.x = cellF.size.width - accessF.size.width - deltaW;
21     self.accessoryView.frame = accessF;
22 }

 

自定义cell的背景

#pragma mark 设置背景
- (void)setupBg
{
    // 1.默认
    UIView *bg = [[UIView alloc] init];
    bg.backgroundColor = [UIColor whiteColor];
    self.backgroundView = bg;

    // 2.选中
    UIView *selectedBg = [[UIView alloc] init];
    selectedBg.backgroundColor = ILColor(237, 233, 218);
    self.selectedBackgroundView = selectedBg;
}

 

posted @ 2014-01-24 19:46  王小飞您  阅读(722)  评论(0编辑  收藏  举报