前天為了一個(gè)分割線(xiàn)粗了的問(wèn)題,真是傷透腦筋。
處理Separator的方式
1.使用系統(tǒng)的Separator時(shí)
重寫(xiě)Cell或者在cell返回的地方設(shè)置也可以,separatorInset設(shè)置成你想要的邊距就行稼钩。自由伸縮哦,填滿(mǎn)的分割先Zero就可以咯达罗。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.separatorInset = UIEdgeInsetsMake(top: 0, left: 10, bottom: 0, right: 10)
self.layoutMargins = UIEdgeInsetsZero
self.preservesSuperviewLayoutMargins = false
}
return self;
}
2.使用CustomSeparator時(shí)
這個(gè)就簡(jiǎn)單點(diǎn)處理了馅扣,就是自己添加一下分割線(xiàn)燕酷。先給UIVIew
加個(gè)分類(lèi)方法劫乱,邊距設(shè)置insets澄者。
- (void)addBottomLineWithColor:(UIColor *)lineColor insets:(UIEdgeInsets)insets {
UIView *bottomLine = [[UIView alloc] init];
bottomLine.backgroundColor = lineColor;
[self addSubview:bottomLine];
[bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(CGFloatFromPixel(1)));
make.bottom.equalTo(self);
make.edges.equalTo(self).insets(insets).priorityLow();
}];
}
然后初始化的時(shí)候調(diào)用就好
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addBottomLineWithColor:[UIColor black8Color] insets:UIEdgeInsetsMake(0, 12, 0, 0)];
}
return self;
}
問(wèn)題來(lái)了
使用Plain類(lèi)型的UITableView時(shí)倒是無(wú)任何問(wèn)題。但是當(dāng)我要用Group類(lèi)型扶认,又要用系統(tǒng)Separator時(shí)侨拦,問(wèn)題就來(lái)了。
用過(guò)Group的朋友都知道辐宾,SectionHeaderView上下會(huì)自動(dòng)出現(xiàn)SeparatorLine狱从,十分便捷,無(wú)需你我動(dòng)手叠纹。然而剛好有的地方是不用SectionHeaderView的季研,高度設(shè)置為0,結(jié)果變成了一個(gè)默認(rèn)高度誉察。所以只好設(shè)置為0.001f咯与涡。
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==0) {
return 0.001;
}
return 8;
}
因此出現(xiàn)了開(kāi)題圖中疊在一起的粗線(xiàn)。持偏。驼卖。搗騰了兩個(gè)小時(shí),任憑我如何擺弄鸿秆,都無(wú)法消除它酌畜。最后無(wú)耐,選擇了自己添加分割線(xiàn)卿叽。
記錄一下檩奠,希望后人少走彎路。