简体   繁体   English

UITableView 分组隐藏部分分隔符

[英]UITableView grouped hide section separator

I know this question has been asked before.我知道这个问题以前有人问过。 But no person on the internet had a working and sufficient answer.但是互联网上没有人有一个有效的和充分的答案。

EDIT Obviously people don't read questions anymore, on SO.编辑显然,人们不再阅读问题了。 So I'm trying to clarify: I want to remove the SEPARATOR.所以我想澄清一下:我想删除 SEPARATOR。 The separator is neither the space above the section, nor the tableViewHeader or tableViewFooterView.分隔符既不是部分上方的空间,也不是 tableViewHeader 或 tableViewFooterView。 It is only the thin line above (fully from left to right).它只是上面的细线(完全从左到右)。

I have a grouped UITableView (I don't want to use a plain styled for many other reasons, take it as it is) which has multiple groups.我有一个分组的UITableView (由于许多其他原因,我不想使用普通样式,照原样)它有多个组。

The first section should not have the separator line on top.第一部分不应在顶部有分隔线。 Setting the separator style of the tableView is not an option, because I do need the other separators.设置 tableView 的分隔符样式不是一个选项,因为我确实需要其他分隔符。

Setting the tableViews tableFooterView is something I often read, but it never worked.设置 tableViews tableFooterView是我经常阅读的内容,但从未奏效。

I used the tableView with static content before and I was able to remove the separator in -[UITableViewController viewDidLoad] using this:我之前使用了带有静态内容的 tableView,并且能够使用以下命令删除-[UITableViewController viewDidLoad]中的分隔符:

- (void)viewDidLoad {
    [[[self headerTableCell] valueForKey:@"_topSeparatorView"] removeFromSuperView];
}

Since I now had to change the tableView to a dynamic one, the IBOutlet property won't work anymore (obviously).由于我现在不得不将 tableView 更改为动态的,因此IBOutlet属性将不再起作用(显然)。

So I tried everything, -[id tableView:willDisplayCell:atIndexPath:] , -[UITableViewCell initWithStyle:reuseIdentifier: , prepareForReuse , awakeFromNib] and some others.所以我尝试了一切, -[id tableView:willDisplayCell:atIndexPath:]-[UITableViewCell initWithStyle:reuseIdentifier:prepareForReuseawakeFromNib]和其他一些。

In any case, this separator is nil.在任何情况下,这个分隔符都是零。 So I need a method that gets called when the complete view hierarchy of the cell is setup.所以我需要一个在单元格的完整视图层次结构设置时被调用的方法。

我从你的情况中得到的你有一个分组的 UITableView 你想要第一部分没有分隔符并且你想在其他部分保留分隔符所以从属性检查器中删除整个 tableview分隔符make Separator : None在故事板中创建自定义 UITableviewCell对于其他部分,并在其末尾添加View ,高度为 1,宽度为整个屏幕(如默认分隔符),这可能不是最好的主意,但这将允许您拥有没有分隔符的第一部分

I faced a similar problem, wanted to remove the last line of the section in grouped table view, I am calling following method in view will appear and on every table reload.我遇到了类似的问题,想删除分组表视图中该部分的最后一行,我在视图中调用以下方法将出现并重新加载每个表。 This is not the exact answer but problem can be solved by just changing y value of dummy view.这不是确切的答案,但只需更改虚拟视图的 y 值即可解决问题。

+(void)removeLastSectionSeparatorForTableView:(UITableView *)tableView
{
    UIView *oldSeparatorView = [tableView viewWithTag:kTagDummySectionSeparator];

    if (oldSeparatorView != nil)
    {
        [oldSeparatorView removeFromSuperview];
    }

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(),
                   ^{

                       UIView *view = [[UIView alloc] init];
                       view.tag = kTagDummySectionSeparator;
                       view.backgroundColor = [UIColor colorWithRed:239.0/255 green:239.0/255 blue:244.0/255 alpha:1.0];//Group table background color
                       view.frame = CGRectMake(0,
                                               tableView.contentSize.height-40,
                                               tableView.bounds.size.width,
                                               2);
                       [tableView addSubview:view];
                   });
}

Maybe this problem is the same as mine before.也许这个问题和我之前的一样。 Finally, my way to solve this problem: set table view delegate 's method (CGFloat)tableView:(UITableView *)tableView heightForHeaderAtSection:(NSInteger)section , then return CGFLOAT_MIN;最后,我解决这个问题的方法是:设置 table view delegate的方法(CGFloat)tableView:(UITableView *)tableView heightForHeaderAtSection:(NSInteger)section ,然后return CGFLOAT_MIN;

add this override function in your Custom Cell Class在您的自定义单元类中添加此覆盖函数

   override func layoutSubviews() {
    super.layoutSubviews()
    for subview in subviews where (subview != contentView && abs(subview.frame.width - frame.width) <= 0.1 && subview.frame.height < 2) {
        subview.removeFromSuperview()
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM