我需要从特定部分中的第一个单元格中删除顶部边框,并从特定部分中的最后一个单元格中删除底部边框。
我在谷歌上搜索了完全隐藏tableView分隔符的解决方案,但我想避免这种方法。
另外,我找到了一种解决方案,可以在单元格即将显示时获取事件以操作分隔符插图。它适用于两个单元格之间的中间分隔符,但不适用于标题/页脚和单元格之间的分隔符。
此外,我尝试查看标题中的子层,但由于这个视图是由我自己创建的,所以我没有找到分隔符视图。
也许我应该在标题内部使用图层?请给我一些提示。
我需要从特定部分中的第一个单元格中删除顶部边框,并从特定部分中的最后一个单元格中删除底部边框。
我在谷歌上搜索了完全隐藏tableView分隔符的解决方案,但我想避免这种方法。
另外,我找到了一种解决方案,可以在单元格即将显示时获取事件以操作分隔符插图。它适用于两个单元格之间的中间分隔符,但不适用于标题/页脚和单元格之间的分隔符。
此外,我尝试查看标题中的子层,但由于这个视图是由我自己创建的,所以我没有找到分隔符视图。
也许我应该在标题内部使用图层?请给我一些提示。
tableHeaderView
即可。
您可以在 viewDidLoad
中这样做:tableView.tableHeaderView = UIView()
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) {
cell.separatorInset.left = cell.bounds.size.width
}
}
对我来说,所有这些视图操作比创建自己的UITableViewCell子类要复杂得多,在其中放置一个高度为1p的UIView作为自己的分隔符,并根据indexPath的行是否是该部分的最后一行,在cellForRowAtIndexPath:中将其隐藏。
一旦您了解如何创建自定义UITableViewCells,这实际上是一项相当容易的工作。而且,它比操作苹果设计更加安全和具有未来性。他们提供了适合整个系统样式的分隔线。并不是他们所做的每件事都旨在最大程度地定制。
对于分隔线,我认为最好要么接受他们的样式,要么自己设计一个分隔符视图的实现。
ViewDidLoad
方法中。 - (void)viewDidLoad {
[super viewDidLoad];
yourTableview.tableFooterView=[[UITableView alloc]initWithFrame:CGRectZero];
}
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ( indexPath.row == tableData.count-2 ) {
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));
}
else {
// do nothing
}
}
要移除底部插图,这个方法对我有效!
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, self.tableView.bounds.width)
Swift 5
隐藏第一个单元格和最后一个单元格的分隔线:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 || indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: tableView.bounds.width)
}
}
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.contentView.superview.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"] && subview.frame.origin.x == 0 ) {
subview.hidden = YES;
}else
{
subview.hidden = NO;
}
}
}