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

}

猜你喜欢

转载自blog.csdn.net/dwz100916007/article/details/88548046
今日推荐