我们在写tableView的时候经常会遇到分割线问题,系统默认分割线总是左偏离15个像素。但是在实际项目中我们可能会让分割线消失,或者居中,或者左偏100等等很多情况。那么在不想自定义分割线的情况下怎么让分割线达到自己的预期目的呢?废话不多说,直接上代码
- (void)viewDidLayoutSubviews
{
if ([my_tableView respondsToSelector:@selector(setSeparatorInset:)])
{
my_tableView.separatorInset = cell_inset;
}
if ([my_tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[my_tableView setLayoutMargins: cell_inset];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:cell_inset];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
{
[cell setSeparatorInset:cell_inset];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
{
[cell setPreservesSuperviewLayoutMargins:NO];
}
}
my_tableView是UITableView对象
为了方便,我们可以把UIEdgeInsets宏定义,或者定义一个静态变量
#define kCELL_INSETS UIEdgeInsetsMake(0, 0, 0, 0)
或
static UIEdgeInsets cell_inset = {0,0,0,0};
这样我们就可以很方便的修改偏移量了