前天為了一個分割線粗了的問題,真是傷透腦筋。
粗了的Separator
處理Separator的方式
1.使用系統的Separator時
重寫Cell或者在cell返回的地方設置也可以,separatorInset設置成你想要的邊距就行。自由伸縮哦,填滿的分割先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時
這個就簡單點處理了,就是自己添加一下分割線。先給UIVIew
加個分類方法,邊距設置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();
}];
}
然后初始化的時候調用就好
- (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;
}
問題來了
使用Plain類型的UITableView時倒是無任何問題。但是當我要用Group類型,又要用系統Separator時,問題就來了。
用過Group的朋友都知道,SectionHeaderView上下會自動出現SeparatorLine,十分便捷,無需你我動手。然而剛好有的地方是不用SectionHeaderView的,高度設置為0,結果變成了一個默認高度。所以只好設置為0.001f咯。
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section==0) {
return 0.001;
}
return 8;
}
因此出現了開題圖中疊在一起的粗線。。。搗騰了兩個小時,任憑我如何擺弄,都無法消除它。最后無耐,選擇了自己添加分割線。
記錄一下,希望后人少走彎路。