天天看點

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];

    }

繼續閱讀