UITableViewCell隐藏分隔符使用iOS的iOS 11失败 [英] UITableViewCell hide separator using separatorInset fails in iOS 11

查看:52
本文介绍了UITableViewCell隐藏分隔符使用iOS的iOS 11失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来在iOS 11之前隐藏单个UITableViewCell分隔符的代码:

This is the code I used to hide the separator for a single UITableViewCell prior to iOS 11:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {


        // Remove separator inset
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, tableView.frame.size.width, 0, 0)];
        }

        // Prevent the cell from inheriting the Table View's margin settings
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }

        // Explictly set your cell's layout margins
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, tableView.frame.size.width, 0, 0)];
        }
    }
}

在此示例中,分隔符对于每个节的第一行都是隐藏的.我不想完全摆脱分隔符-仅针对某些行.

In this example, the separator is hidden for the first row in every section. I don't want to get rid of the separators completely - only for certain rows.

在iOS 11中,以上代码无效.单元格的内容完全推到右侧.

In iOS 11, the above code does not work. The content of the cell is pushed completely to the right.

有没有一种方法可以为iOS 11中的单个UITableViewCell隐藏分隔符的任务?

Is there a way to accomplish the task of hiding the separator for a single UITableViewCell in iOS 11?

让我事先澄清一下,我确实知道我可以使用以下代码隐藏整个UITableView的分隔符(以希望避免指示我执行此操作的答案):

Let me clarify in advance that I do know that I can hide the separator for the entire UITableView with the following code (to hopefully avoid answers instructing me to do this):

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

同样,在下面的注释后进行澄清,如果我完全包含setSeparatorInset行,则代码将执行完全相同的操作.因此,即使只有这一行,该单元格的内容也会一直推到最右边.

Also to clarify after a comment below, the code does exactly the same thing if I include the setSeparatorInset line at all. So even with only that one line, the content of the cell is pushed all the way to the right.

推荐答案

如果您不希望在 UITableViewCell 中添加自定义分隔符,我可以向您展示另一种解决方法.

If you are not keen on adding a custom separator to your UITableViewCell I can show you yet another workaround to consider.

因为分隔符的颜色是在 UITableView 级别上定义的,所以没有明确的方法可以针对每个 UITableViewCell 实例更改它.它不是Apple想要的,您唯一可以做的就是破解它.

Because the color of the separator is defined on the UITableView level there is no clear way to change it per UITableViewCell instance. It was not intended by Apple and the only thing you can do is to hack it.

您需要做的第一件事就是访问分隔符视图.您可以使用此小扩展程序来做到这一点.

The first thing you need is to get access to the separator view. You can do it with this small extension.

extension UITableViewCell {
    var separatorView: UIView? {
        return subviews .min { $0.frame.size.height < $1.frame.size.height }
    }
}

有权访问分隔符视图时,必须适当配置 UITableView .首先,将所有分隔符的全局颜色设置为 .clear (但不要禁用它们!)

When you have an access to the separator view, you have to configure your UITableView appropriately. First, set the global color of all separators to .clear (but don't disable them!)

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.separatorColor = .clear
}

接下来,为每个单元格设置分隔符颜色.您可以根据自己的喜好为每种颜色设置不同的颜色.

Next, set the separator color for each cell. You can set a different color for each of them, depends on you.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SeparatorCell", for: indexPath)
    cell.separatorView?.backgroundColor = .red

    return cell
}

最后,对于该部分的每一行,将分隔符颜色设置为 .clear .

Finally, for every first row in the section, set the separator color to .clear.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        cell.separatorView?.backgroundColor = .clear
    }
}

为什么起作用

首先,让我们考虑 UITableViewCell 的结构.如果打印出单元格的 subviews ,您将看到以下输出.

Why it works

First, let's consider the structure of the UITableViewCell. If you print out the subviews of your cell you will see the following output.

<UITableViewCellContentView: 0x7ff77e604f50; frame = (0 0; 328 43.6667); opaque = NO; gestureRecognizers = <NSArray: 0x608000058d50>; layer = <CALayer: 0x60400022a660>>
<_UITableViewCellSeparatorView: 0x7ff77e4010c0; frame = (15 43.5; 360 0.5); layer = <CALayer: 0x608000223740>>
<UIButton: 0x7ff77e403b80; frame = (0 0; 22 22); opaque = NO; layer = <CALayer: 0x608000222500>>

您将看到一个包含内容,分隔符和附件按钮的视图.从这个角度来看,您只需要访问分隔符视图并修改其背景.不幸的是,这并不容易.

As you can see there is a view which holds the content, the separator, and the accessory button. From this perspective, you only need to access the separator view and modify it's background. Unfortunately, it's not so easy.

让我们看一下视图调试器中相同的 UITableViewCell .如您所见,有两个分隔符视图.您需要访问调用 willDisplay:时不存在的最下面的一个.这是第二个hacky部分发挥作用的地方.

Let's take a look at the same UITableViewCell in the view debugger. As you can see, there are two separator views. You need to access the bottom one which is not present when the willDisplay: is called. This is where the second hacky part comes to play.

当您检查这两个元素时,您会看到第一个(从顶部开始)的背景色设置为 nil ,第二个的背景色设置为您指定的值整个 UITableView .在这种情况下,带有颜色的分隔符将覆盖没有颜色的分隔符.

When you will inspect these two elements, you will see that the first (from the top) has a background color set to nil and the second has a background color set to the value you have specified for entire UITableView. In this case, the separator with the color covers the separator without the color.

要解决此问题,我们必须扭转"局势.我们可以将所有分隔符的颜色设置为 .clear ,以显示我们可以访问的分隔符.最后,我们可以将可访问分隔符的背景色设置为所需的颜色.

To solve the issue we have to "reverse" the situation. We can set the color of all separators to .clear which will uncover the one we have an access to. Finally, we can set the background color of the accessible separator to what is desired.

这篇关于UITableViewCell隐藏分隔符使用iOS的iOS 11失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆