UITableView分割线Separator这件小事

作者: 飛呈Geek | 来源:发表于2016-06-02 00:26 被阅读2438次

    前天为了一个分割线粗了的问题,真是伤透脑筋。


    粗了的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;
    }
    

    因此出现了开题图中叠在一起的粗线。。。捣腾了两个小时,任凭我如何摆弄,都无法消除它。最后无耐,选择了自己添加分割线。
    记录一下,希望后人少走弯路。

    相关文章

      网友评论

        本文标题:UITableView分割线Separator这件小事

        本文链接:https://www.haomeiwen.com/subject/kcahdttx.html