iOS-关于UITableViewCell样式分界线去掉左边空白的方法

iOS-关于UITableViewCell样式分界线去掉左边空白的方法

默认UITableViewCell是留有图标区域的,但是要是不加图标,就会在左边留有一段空间,很不好看,下面有两种方法填充这段空白:

  1. 重新定义一个UITableViewCell的子类,在.m的实现里填上下边这段方法,给它画一个分割线
    -(void)drawRect:(CGRect)rect
    {
    [super drawRect:rect];
    CGContextRef cxt = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(cxt, 0.5);
    CGContextSetStrokeColorWithColor(cxt, [UIColor colorWithWhite:0.910 alpha:1.000].CGColor);
    CGContextMoveToPoint(cxt, 0.0 , self.frame.size.height - 0.5);
    CGContextAddLineToPoint(cxt,self.frame.size.width , self.frame.size.height - 0.5);
    CGContextStrokePath(cxt);
    }

千万记得在UITableView上关掉原本的Cell分割线样式

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

2.设置分割线的偏移量为0;
定义 tableview 时设置
self.tableView.separatorInset = UIEdgeInsetsZero;
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
设置 tableview 的代理方法
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

if ([cell respondsToSelector:@selector(separatorInset)]) {
    [cell setSeparatorInset:UIEdgeInsetsZero];
}

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值