美文网首页
通过类别的方式来显示cell中的线条

通过类别的方式来显示cell中的线条

作者: 羊妞麻麻 | 来源:发表于2019-03-12 14:56 被阅读0次

    设计师往往都会对UITableViewCell上面的线做文章,下面通过分类的方式快速显示。

    @interface UIView (FMPCoomon)
    - (void)showTopSeparateLine:(BOOL)bShow edgeInset:(UIEdgeInsets)separatorInset;
    - (void)showBottomSeparateLine:(BOOL)bShow edgeInset:(UIEdgeInsets)separatorInset;
    - (UIView*)getTopSeparateLine;
    - (UIView*)getBottomSeparateLine;
    @end
    
    #define CELL_TOPLINE        112301
    #define CELL_BOTLINE        112302
    
    @implementation UIView (FMPCoomon)
    - (UIView*)addTopLine:(UIEdgeInsets)separatorInset {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(separatorInset.left, separatorInset.top, self.width - separatorInset.left - separatorInset.right, 0.5)];
        line.backgroundColor = [UIColor SCTableSeparateColor];
        line.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        line.tag = CELL_TOPLINE;
        [self addSubview:line];
        return line;
    }
    
    - (UIView*)addBottomLine:(UIEdgeInsets)separatorInset {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(separatorInset.left, self.height - .5 - separatorInset.bottom, self.width - separatorInset.left - separatorInset.right, 0.5)];
        line.backgroundColor = [UIColor SCTableSeparateColor];
        line.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        line.tag = CELL_BOTLINE;
        [self addSubview:line];
        return line;
    }
    
    - (void)showTopSeparateLine:(BOOL)bShow edgeInset:(UIEdgeInsets)separatorInset {
        UIView *line = [self viewWithTag:CELL_TOPLINE];
        if (!line)
            line = [self addTopLine:separatorInset];
        else if (bShow)
            line.frame = CGRectMake(separatorInset.left, separatorInset.top, self.width - separatorInset.left - separatorInset.right, 0.5);
        
        line.hidden = !bShow;
    }
    
    - (void)showBottomSeparateLine:(BOOL)bShow edgeInset:(UIEdgeInsets)separatorInset {
        UIView *line = [self viewWithTag:CELL_BOTLINE];
        if (!line)
            line = [self addBottomLine:separatorInset];
        else if (bShow)
            line.frame = CGRectMake(separatorInset.left, self.height - .5 - separatorInset.bottom, self.width - separatorInset.left - separatorInset.right, 0.5);
        
        line.hidden = !bShow;
    }
    
    - (UIView*)getTopSeparateLine {
        UIView *line = [self viewWithTag:CELL_TOPLINE];
        if (!line)
            line = [self addTopLine:UIEdgeInsetsZero];
        return line;
    }
    - (UIView*)getBottomSeparateLine {
        UIView *line = [self viewWithTag:CELL_BOTLINE];
        if (!line)
            line = [self addBottomLine:UIEdgeInsetsZero];
        
        return line;
    }
    @end
    
    

    具体使用:

    [cell.contentView showTopSeparateLine:indexPath.row != 0 edgeInset:UIEdgeInsetsZero];
    
    [self.headerView showTopSeparateLine:YES edgeInset:UIEdgeInsetsZero];
    [self.headerView showBottomSeparateLine:YES edgeInset:UIEdgeInsetsZero];
    self.tableView.tableHeaderView = self.headerView;
    
    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *identifier = @"BillTableViewCell";
        BillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [BillTableViewCell loadFromXibWithIdentifier:identifier];
        }
        if (indexPath.row == ([tableView numberOfRowsInSection:indexPath.section] - 1)) {
            [cell showBottomSeparateLine:YES edgeInset:UIEdgeInsetsZero];
        } else if (indexPath.row == 0) {
            [cell showTopSeparateLine:YES edgeInset:UIEdgeInsetsZero];
            [cell showBottomSeparateLine:YES edgeInset:UIEdgeInsetsMake(0, 15, 0, 0)];
        } else {
            [cell showTopSeparateLine:NO edgeInset:UIEdgeInsetsZero];
            [cell showBottomSeparateLine:YES edgeInset:UIEdgeInsetsMake(0, 15, 0, 0)];
        }
        NSArray *dataArr = [self dataArrWith:tableView];
        NSArray *groupArr = dataArr[indexPath.section];
        BillInfoModel *info = groupArr[indexPath.row];
        [cell updateViewWithData:info];
        return cell;
    }
    

    相关文章

      网友评论

          本文标题:通过类别的方式来显示cell中的线条

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