iOS-UITableViewCell自定义分割线

在最近的自定义UITableViewCell中遇到分割线间距问题,找了几种解决方法:

设置UITableViewCell分割线间距

方法一:

// 系统版本大于7.0
TableView.separatorInset = UIEdgeInsetsMake(0,0, 0, 0);     //top left bottom right 上左下右边距相同      
TableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine

方法二:

-(void)viewDidLayoutSubviews {  
    if ([_tableview respondsToSelector:@selector(setSeparatorInset:)]) {  
        [_tableview setSeparatorInset:UIEdgeInsetsZero];  
    }  
    if ([_tableview respondsToSelector:@selector(setLayoutMargins:)])  {  
        [_tableview setLayoutMargins:UIEdgeInsetsZero];  
    }  
}  
  
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{  
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {  
        [cell setLayoutMargins:UIEdgeInsetsZero];  
    }  
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){  
        [cell setSeparatorInset:UIEdgeInsetsZero];  
    }  
}  

方法三:

//第一步:    
//UITableView去掉自带系统的分割线    
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;    
    
//第二步:    
//在自定义的Cell里重写drawRect:方法    
#pragma mark - 绘制Cell分割线    
- (void)drawRect:(CGRect)rect {    
    CGContextRef context = UIGraphicsGetCurrentContext();    
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);    
    CGContextFillRect(context, rect);    
    //上分割线,    
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1].CGColor);    
    CGContextStrokeRect(context, CGRectMake(0, 0, rect.size.width, 1));    
    //下分割线    
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1].CGColor);    
    CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));    
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,261评论 25 709
  • 从现在开始,不能再玩了,努力,相信自己
    戛儿阅读 1,092评论 0 0
  • 如果可以的话,管好自己的行为管好自己的嘴。 把生活回归本真,简单即是大智慧。 真诚,善良。 晚安
    WuliXY阅读 949评论 0 0
  • 每当和别人讨论起体重的问题,我总会说:“上小学的时候,我也是很瘦的。”好像只有这样才能证明我是爹妈亲生的😓 是从上...
    十月茉阅读 994评论 0 0
  • 今天看了几个克里希那穆提的视频。刚看的当下,有被震惊到,甚至有想哭的冲动。但是不得不说,也许是我没有太多理解到,或...
    雪少阅读 4,187评论 3 48