我們在寫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};
這樣我們就可以很友善的修改偏移量了