iOS 中隐藏UITableView最后一条分隔线

    xiaoxiao2021-11-07  42

    如何优雅的隐藏UITableView中最后一条分割线? 这个问题是很常见,却又不太容易解决的。 可能通常的做法都是隐藏UITableView的分割线,自定义一条。 最近在使用弹出菜单的时候,同样遇到了这个问题。

    需求场景,做一个类似微信公众号,底部菜单弹出的菜单视图。 而这样的菜单中的tableView一般contentSize等于其frame.size,并且tableView的scrollEnabled为NO。

    我想了一种方法(创建一个高度为1px的UIView,盖住tableView的底部1px):

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

    然而,作者给出了一个很优雅的做法,只需要添加几行代码即可,关键代码如下:

        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,然后将分割线偏移出屏幕外即可。

    注意: 经过测试,上面这种设置cell的separatorInset,来让最后一条分割线不显示出来的做法, 对自定义的Cell有效; 对于UITableViewCell,修改了separatorInset,会导致textLabel也随着偏移。

    转载请注明原文地址: https://ju.6miu.com/read-678092.html

    最新回复(0)