表是我们开发中经常使用的一个控件,而我们在开发中万恶的UI总会给我们指出各种各样无语的问题。比如要在单元格的下面添加一条2px的线,美名曰好看。用系统自带的效果人家还不同意,美名曰线要到头。好吧,给大家一个方法,让分割线自由长度随你心意。当然,我看到网上已经有这方面的回答了。但是我还是自己总结一下,纠结了自己那么久的问题,记录一下。
首先,demo的地址链接:https://github.com/sunyunfei/DividerDemo.git
开始代码部分,基本的创建就不说了,直接看分割线设置部分:
/**
* 分割线设置代码
*/
-(void)viewDidLayoutSubviews
{
UIEdgeInsets edgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:edgeInset];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:edgeInset];
}
}
/**
* 行将要显示的时候调用
*
* @param tableView <#tableView description#>
* @param cell <#cell description#>
* @param indexPath <#indexPath description#>
*/
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIEdgeInsets edgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:edgeInset];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:edgeInset];
}
}