天天看点

TableViewCell的分割线显示不完全处理方法

1、加方法,隐藏分割线

- (void)viewDidLoad

{

    [super viewDidLoad];

    //设置tableView不能滚动

    [self.tableView setScrollEnabled:NO];

    //在此处调用一下就可以啦 :此处假设tableView的name叫:tableView

    [self setExtraCellLineHidden:self.tableView];

}

-(void)setExtraCellLineHidden: (UITableView *)tableView

{

    UIView *view = [UIView new];

    view.backgroundColor = [UIColor clearColor];

    [tableView setTableFooterView:view];

}

2、cell显示内容时候,用图片替换

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 staticNSString *cellID =@"Cell";

    _cell = [tableViewdequeueReusableCellWithIdentifier:cellID];

    if (_cell ==nil) {

        _cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID];

     UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width,1.0f)];

    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];

    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];

separatorLine.hidden = NO;

    }

   UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];

   separatorLine.hidden = NO;

3、自定义的cell,在上面画自己画一条线和屏幕宽度一样长

UITableView中将分割线样式改为None 

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

自定义UITableViewCell中复写- (void)drawRect:(CGRect)rect方法 

- (void)drawRect:(CGRect)rect { 

CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect); //上分割线

CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"ffffff"].CGColor);

CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1)); //下分割线 CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e2e2e2"].CGColor); CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1));

 }

4、重写父类的方法

-(void)viewDidLayoutSubviews {

    if ([self.mytableview respondsToSelector:@selector(setSeparatorInset:)]) {

        [self.mytableview setSeparatorInset:UIEdgeInsetsZero];

    }

    if ([self.mytableview respondsToSelector:@selector(setLayoutMargins:)])  {

        [self.mytableview setLayoutMargins:UIEdgeInsetsZero];

    }

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

继续阅读