如何优雅的隐藏UITableView中最后一条分割线?git
这个问题是很常见,却又不太容易解决的。github
可能一般的作法都是隐藏UITableView的分割线,自定义一条。bash
最近在使用弹出菜单的时候,一样遇到了这个问题。微信
需求场景,作一个相似微信公众号,底部菜单弹出的菜单视图。 而这样的菜单中的tableView通常
contentSize
等于其frame.size
,而且tableView的scrollEnabled
为NO。测试
我想了一种方法(建立一个高度为1px的UIView,盖住tableView的底部1px):spa
UIView *lineView = [self viewWithTag:201];
if (!lineView) {
lineView = [[UIView alloc] initWithFrame:CGRectZero];
}
lineView.frame = CGRectMake(5, menuRect.size.height-2, menuRect.size.width - 10, 2);
lineView.tag = 201;
lineView.backgroundColor = [FTPopOverMenuConfiguration defaultConfiguration].tintColor;
[self insertSubview:lineView aboveSubview:self.menuTableView];
复制代码
然而,做者给出了一个很优雅的作法,只须要添加几行代码便可,关键代码以下:code
if (indexPath.row == _menuStringArray.count-1) {
menuCell.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, 0);
}else{
menuCell.separatorInset = UIEdgeInsetsMake(0, FTDefaultMenuTextMargin, 0, 10+FTDefaultMenuTextMargin);
}
复制代码
咱们只须要在CellForRow
方法中判断是最后一个cell,而后将分割线偏移出屏幕外便可。get
注意: 通过测试,上面这种设置cell的
separatorInset
,来让最后一条分割线不显示出来的作法, 对自定义的Cell有效; 对于UITableViewCell,修改了separatorInset
,会致使textLabel也随着偏移。it